Python Study --- Learning advanced functions
Higher order function : The function name can be passed as input , Function names can also be returned as return values
Function names can be reassigned , Because it's a variable in itself
A function itself is an object ,
The variable name of the function f It points to the function itself , After parenthesis f() You can execute its contents
The variable name of a function can also be used as a function parameter , It can also be used as the return value of a function
def add(x, y, f):
return f(x) + f(y)
res = add(3, -6, abs)
print(res) # 9
############################################################
def f(n):
return pow(n, n)
def foo(a, b, fun):
return fun(a) + fun(b)
def hello():
print('hello world')
# Function names can be reassigned , Because it's a variable in itself
hhh = foo
# The function name can be executed as a parameter
v = foo(2, 3, f)
print(v)
good = hhh(2, 3, f)
print(good)
Function return value
To get the execution result of a function , You can use it return Statement returns the result
Be careful :
1. Function in the process of execution as long as return sentence , It stops execution and returns the result , It can also be understood as return The statement represents the end of the function
2. If... Is not specified in the function return, The return value of this function is None
3. return Multiple objects ,Python The multiple objects will be assembled into a tuple as a whole 【 Tuples 】 Results output .
return 1,2,3,'name' ==> (1,2,3,'name')
python The scope of 4 In this case :
L:local, Local scope , That is, the variables defined in the function ;
E:enclosing, The local scope of the nested parent function , That is, the local scope of the parent function containing this function , But it's not the whole thing ;
G:globa, Global variables , Variables defined at the module level ;
B:built-in, Variables in the system fixed module , such as int, bytearray etc. .
The priority order of the search variables is : Scope local > Outer scope > The global in the current module >python Built-in scope , That is to say LEGB.
Of course ,local and enclosing Is relative ,enclosing Variables are also relative to the upper level local.
x = int(2.9) # int built-in
g_count = 0 # global
def outer():
o_count = 1 # enclosing
def inner():
i_count = 2 # local
print(o_count)
# print(i_count) Can't find
inner()
outer()
# print(o_count) # Can't find
Scope produces
stay Python in , Only modules (module), class (class) And function (def、lambda) To introduce a new scope , Other code blocks ( Such as if、try、for etc. ) No new scopes will be introduced
Local variable modify global variable : global and nolocal Keyword modification
global keyword ;
When the modified variable is in global scope (global Scope ) Upper [global Cannot nest on scope ], Then use global Say it first
count = 10
def outer():
# global count = 90 SyntaxError: invalid syntax Grammar mistakes
global count # Local variables want to modify global variables , Variable must be declared as global
print(count)
count = 5 # Local variables want to modify global variables , Variable must be declared as global
print(count)
outer()
nolocal keyword ;
Modify nested scope (enclosing Scope , The outer layer is not a global scope ) The variables in the
def outer():
count = 10
def inner():
nonlocal count # Use nolocal After declaration , You can modify the external count The variable
count = 20
print(count)
inner()
print(count)
outer()
#20
#20
Scope summary :
(1) Variable lookup order :LEGB, Scope local > Outer scope > The global in the current module >python Built-in scope ;
(2) Only modules 、 class 、 And functions to introduce new scopes ;
(3) For a variable , The internal scope is declared first, and then the external variable is overridden , Do not declare direct use of , The variables in the external scope are used ;
(4) Internal scope to modify the value of an external scope variable , Global variables use global keyword , Nested scope variables use nonlocal keyword .nonlocal yes python3 New keywords , With this keyword , It's perfect for closures .
author : Small a ninety-seven
-------------------------------------------
Individuality signature : Everything is good in the end , If it's not good , That means things haven't come to the end yet ~
The copyright of this article belongs to the author 【 Small a ninety-seven 】, Welcome to reprint , However, this statement must be retained without the consent of the author , And in the article page obvious position gives the original link , Otherwise, the right to pursue legal responsibility is reserved !