A python function can return a value or object. If no value is returned, none is returned by default. If multiple objects are returned, a single tuples are actually returned.
A function is passed through a value assignment (Object reference. Callers and functions reference shared objects, but do not need aliases. Changing the parameter name in the function does not change the variable name in the caller, but changing the passed variable object can change the object shared by the caller.
A function name is a reference to an object. You can freely assign this object to another name and call it through any reference.
Variable scope:
The function defines the local scope, while the module defines the global scope. Variables correspond to three different scopes:
1. If a variable is assigned a value in def, it is located in this function.
2. If a variable is assigned a value in a nested def, for nested functions, it is non-local
3. If a value is assigned outside def, it indicates that the entire file is global.
If you want to assign a value to the variable name that is located at the top of the module file within the function, you must declare it through the global statement within the function. Note that changing the object in the original field does not divide the variable into local variables. In fact, only the variable name can be assigned a value. For example, l is assigned as a list on the top layer. In a function like L. append (x), L is not divided into local variables, but l = x. Remember: modifying an object does not assign a value to a name.
Assign a value to the variable name in the function to divide it into local variables. The local variables must be defined before reference !!!
Variable name reference is divided into three scopes for search: local, function, global, and built-in. (Legb)
When a variable name is assigned to a function (instead of being referenced in an expression), Python always creates or modifies the name of a variable in the local scope, unless it is declared as a global variable in that function.
Lambda: Python allows you to use Lambda keywords to create anonymous functions. You can call Lambda to generate a function object that can be used like other functions using appropriate expressions (rather than statements. Lambda expressions introduce new local scopes.
Defining a function in a function is called a closure. If a variable in the external scope (but not in the global scope) is referenced in an internal function, the internal function is considered as a closure. Variables defined in external functions that are referenced or used by internal functions are called free variables. Eg:
Def maker (n ):
Def action (x ):
Return x ** n
Return Action
F = maker (2) # F: <function action at...>
F (3) #9
# The external function maker generates and returns a nested function, but does not call this nested function. Nested functions remember the value of N.
# In fact, N in the local scope is retained as the status information
You can also use classes to replace closures.
Function parameters:
Func (value)
|
Caller
|
Common parameters: match by location
|
Func (name = value)
|
Caller
|
Keyword parameter: match by variable name
|
Func (* SEQ)
|
Caller
|
Pass all objects and use them as independent location-based parameters (unpackage)
|
Func (** dict)
|
Caller
|
Pass all the keywords/Values in pairs and use them as independent keyword parameters (unpack)
|
Def func (name)
|
Function
|
Common parameters: match by location or variable name
|
Def func (name = value)
|
Function
|
Default parameters |
Def func (* name)
|
Function
|
Match and collect all parameters containing locations and pass them as tuples to functions (Collection)
|
Def func (** name)
|
Function
|
Match and collect all parameters containing locations and pass them as dictionaries to functions (Collection)
|
The complete statement for function calls allowed in python is:
Func (value, name = value, * seq, ** dict)
The complete statement in the function definition header is:
Def func (name, name = value, * Name, ** name)
The Declaration sequence of function a to call function B: function B can be declared either before or after function, make sure that the statement that calls function a is declared after function B.
Function attribute: you can attach user-defined attributes to a function. Eg: function func, func. Count = 0. Variable names such as Count are local for a function, but their values are retained after the function exits.
Built-in functions:
Apply (func [, nkw] [, KW]): use an optional parameter to call func. nkw is a non-Keyword function, and kW is a keyword function. The return value is the return value of the function call.
Filter (func, SEQ): Call a Boolean function to iterate over the elements in each seq. Return a sequence of elements that make the return value of the function true.
Map (func, seq1 [, seq2...]): Act the function on each element of a given sequence and provide the return value using a list. If func is none, func is represented as an identity function, returns a list of N tuples containing the collection of elements in each sequence (same as zip ).
Reduce (func, seq [, int]): acts binary functions on the elements of the seq sequence, each carrying a pair (previous results and the next sequence element) continuously apply the existing results and the next value to the subsequent results, and finally reduce our sequence into a single return value. If the initial value init is given, the first comparison is the init and the first sequence element instead of the first two elements of the sequence.
Partial function application: the first parameter is fixed, and another function object with n-1 parameters is returned.
from operator import add
from functools import partial
add1=partial(add,1) #add1(x)==add(1,x)
Pay attention to the keyword parameters when using partial functions.
Python is a local variable for static detection of Python. When the def code is compiled, it is not detected by finding a value assignment statement at runtime. That is to say, once a value assignment statement is found in a function, its variables are considered as local variables, even after the value assignment is referenced. (If the value assignment is referenced, if there is no global declaration, the reference will report an error)
This article from the "Lotus miss" blog, please be sure to keep this source http://liandesinian.blog.51cto.com/7737219/1545103
Function and functional programming