Python functions are a very important point of knowledge, and this article gives you complete mastery of functions!

Source: Internet
Author: User
Tags define local function definition python decorator

1. What is a function

2. Definition and invocation of functions

Incoming group: 125240963 to get dozens of sets of PDFs Oh!

2.1 Defining functions

The function definition rules are as follows:

2.2 Parameters in the function

    • The role of parameters
    • function to organize code blocks with independent functions into a small module that is called when needed
    • function parameters, increase the versatility of the function, for the same data processing logic, can adapt to more data
    • 1. Within the function, use parameters as variables to perform the required data processing
    • 2. When a function is called, the data that you want to process inside the function is sorted by the order of the parameters defined by the function.
    • Parameter passing
    • arguments, formal parameters
    • Formal parameters: When defining a function, the arguments in parentheses are used to receive parameters,
    • Inside a function, it is used as a variable.
    • Arguments: used when calling a function, parameters in parentheses are used to transfer data to the inside of the function.

EXECUTE as follows:

Defines a function to add two numbers

Defines a function whose function is to implement the 99 multiplication table

Define a function that implements the asterisk * display for the five rows of five columns

2.3 Invocation of functions

Call Mutil_list

Call Sum_1sum

2.4 Return value of function

    • return value of the function
    • In program development, sometimes you want a function to be executed (for example: a thermometer function, to return to my > 's current temperature), to tell the caller a result so that the caller can follow up on a specific result
    • The return value is the result of a function that finishes work and finally gives the caller
    • Use the return keyword in a function to return the result
    • Call the function side, you can use the variable to receive the return result of the function

2.5 Making cross-module calls

Writing all of the content in one text is too cumbersome and not conducive to troubleshooting, at which point the function can be separated from the call, which facilitates writing to troubleshoot errors.

Do the following

3. Variables in the function

3.1 Local variables

The variables defined inside the function are local variables and cannot be called in other locations; different functions can define local variables of the same name, but each use does not have the effect of affecting the local variables inside the function, and temporarily save the data that needs to be used inside the function.

Note: Because the names of local variables within each function do not affect the similarity, it is best to define variables of different names for ease of distinction so as to distinguish

You can turn the above function into:

Global variables can also be re-assigned

You can use global to declare, the function calls the global variable, in the assignment statement, the global variable value is modified

Note: When making a variable definition, it is required to differentiate the variable name, the global variable and the local variable name, and at development time, define the global variable at the top of all functions.

4 References and adorners for functions

The first thing to understand is that a variable is stored separately from the data stored in a variable that stores the return value of the memory address function of the data, and the memory address of the variable that is returned can be viewed by ID (variable name)

For example:

4.1 Decorators

Detailed understanding of the adorner can be used to understand the Python decorator's popular understanding

    • Decorative Device
    • Concept: Passing a function as an argument to a function
    • Returns a stronger version of the function
    • is essentially a function that returns a function
    • "Adding functionality to a function without altering the original function"

The simplest decorator

Example:

Get to the bar!

Python functions are a very important point of knowledge, and this article gives you complete mastery of functions!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.