Home  >  Article  >  Backend Development  >  Analysis of Python’s understanding of functions

Analysis of Python’s understanding of functions

高洛峰
高洛峰Original
2017-03-14 13:26:081164browse

Definition of function

def functionname(arg):

Suite

Return [expression]

1.def Keywords when defining function

2.functionname function name

3.argThe parameter name of the function, pass Different parameters are entered, and the return value is also different. The function of the function can be realized through the passing of parameters.

4.Suite is the code segment that implements the function function

5.The expressionfollowed by return is the return of the function.

Function call

functionname(arg) Function name (parameter)

After the function is called, the value returned by the return expression is the value of the return expression

Function call The parameters at the time must be passed strictly according to the parameters at the time the function was defined.

Return is followed by an expression and cannot be followed by an assignment statement.

Parameters of the function

Required parameters: (when defining the function)

The parameters of the function only have parameter names and no values ​​when they are defined. This way of definition makes the function When calling, you must pass in parameters to call the function normally. The number and order of required parameters must be consistent with the definition, unless keyword parameters are used when calling, and the order of parameters can be changed.

Default parameters: (when the function is defined)

The parameters of the function are given initial values ​​when they are defined. At this time, if the parameter is not passed a value to this parameter when it is called, the function will Use the default value when defined. If a value is passed to this parameter, the default value will be discarded and the value you passed in will be used.

Keyword parameters: (when calling a function)

Keyword parameters refer to when a function is called, when passing parameters, the parameter names and corresponding values ​​when defining the function are passed into the function together. , then the order of the parameters passed in does not need to be considered.

Indefinite length parameters: (can be used for both definition and call)

The function uses

*args representative element when the number of parameters to be passed in is not sure when defining. Group, args is the name of the tuple, and the value passed in is the element of this tuple. This parameter does not accept keyword parameters.

**kwargs represents the dictionary, kwargs is the name of the dictionary, use keyword parameters when calling, use the parameters as elements of the dictionary, the parameter name is the key of the dictionary, and the parameter value is the value of the dictionary.

These two parameters can be used at the same time, but *args must be in the front and kwargs in the back. When the function is called, the directly passed value must be placed in front of the keyword parameters.

*args and **kwargs can also be used when calling functions.

When we know the number and order of parameters when defining the function, we can combine the parameters that need to be passed in Array into a tuple, and use *+tuple name to pass the parameters. Call functions.

We use the parameter name of the parameter that needs to be passed in as the key of the dictionary, the value of the parameter as the value of the dictionary, and then pass the dictionary composed of the parameters that need to be passed in using ** + dictionary name. Parameters to call the function

Note: When calling a function, the parameters must be passed according to the number and type of parameters when the function is defined. When the value is passed, be sure to know what the parameters are when the function is defined. Type parameters, only when the parameters passed in can match the definition can the return value of the function be correctly called .

GlobalVariablesand local variables

Definition:

Global variables: those defined outside the function with global scope Variables can be accessed throughout the entire program

Global variables can be accessed within the function but cannot be modified within the function.

Local variables: Variables defined inside a function that have a local scope can only be accessed within the function in which they are declared.

The scope of the variable:

Where the variable is assigned determines the area in which the variable acts.

global keyword:

Use the global keyword in the function to declare that this variable is a global variable. When using global to modify this variable in this function, it will This causes the value of this global variable to be modified every time this function is called.

Anonymous function

Basic form:

lambda arg: expression

Use the keyword lambda, arg is the function parameter, expression It is the return value of an anonymous function

It cannot contain control structures or return statements. The returned value is only the value obtained after the expression is calculated.

Using lambda can save the process of function definition and make the code more streamlined.

For some functions that only need to be used once or twice, using lambda does not require thinking about function naming.

Built-in functions filter, map

Inline functions and closures

Concept

Functions are nested inside For a function, the return value of the outer function returns to the function inside. At this time, the function inside can access the variables of the outside function. We cannot directly call the function inside from outside the function, but we can use the return value of the outside function. Calling the function inside, we call this phenomenon closure.

Decorator

uses the nesting of functions . The parameter passed in by the outer function is a function object , and the inner function pair is passed in This function is processed and then returned to the processed function.

Recursive function

Callback function

The above is the detailed content of Analysis of Python’s understanding of functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn