table of Contents
Object Functions
Everything in python objects
Four functions function object
Quote
def f1():
print('from f1')
f1() #调用函数
print(f1)
print('*'*50)
f = f1 # 将函数名f1赋值给f
f() # f也可以调用函数
print(f)
from f1
<function f1 at 0x000001FB05534620>
**************************************************
from f1
<function f1 at 0x000001FB05534620>
Function name corresponding to a variable name, the value returned by the function body receiving both, can be assigned.
As an argument to a function
def f1():
print('from f1')
def f2(a):
return a
f3 =f2(f1)
f3()
print(f3)
from f1
<function f1 at 0x00000275FA914620>
It can be used as the return value of the function
def f1():
print('from f1')
def f2():
return f1
f3 =f2()
f3()
print(f3)
from f1
<function f1 at 0x0000014931304620>
Types of elements can be used as a container
def f1():
print('from f1')
l = [1,2,3,f1]
l[3]()
from f1
Nested functions
Nested function definition
Internal function defined functions can not be used inside a function defined in an external function.
Nested function calls
from math import pi
def circle(r,action):
if action == 'p':
def perimeter():
return 2 * pi * r
res = perimeter()
elif action == 'a':
def area():
return pi * r**2
res = area()
else:
return ' '
return res
result = circle(2,'p')
print(result)
12.566370614359172
Namespace and scope
Namespaces
Space binding relationship between variable name and variable storage memory (storage space variable names), this space is called a namespace.
Built-in namespace
Built-in namespace: Store python interpreter that comes with the name, such as: int, float, len, etc.
len()
int()
Life cycle: when the interpreter started to take effect, fail when the interpreter is closed.
Global name space
Global namespace: In addition to the built-in and a local name, the rest are stored in the global namespace, such as: x, func, l, z
Life cycle: when the file is executed into effect, expire after file execution.
The local name space
The local name space: the name of the function body used to store generated during the function call.
def f1():
x = 1
def f2(): # f2就是局部名称空间
pass
f2()
Life cycle: when the file function call in force, after the failure of the function execution.
Load order (compulsory examinations)
Built-in namespace -> global namespace -> local name space
Find the order (compulsory examinations)
Find the current namespace, and then gradually up if the current is localized, the order is local -> Global -> Built-in
Scope
Scope: regional role
Scope function relationships in the definition phase have been identified well
There may be between function and function variables with the same name, but this has nothing to two variables, different scopes
Global scope
+ Applies to global built, can modify the built-in global i.e., built-in can also be modified globally
Local scope
It applies only to the local area, only a partial acquisition in locally defined name
x =100
def f1():
x =1
print(x) # 定义阶段
f1()
1
global local modify global (understanding, common interview)
x =100
def f1():
global x
x =1
# print(x)
f1()
print(x)
1
into the local external nonlocal
xxxxxxxxxx9 1 def f1():2 x =13 def f2():4 nonlocal x 5 x =36 f2()7 print(x)8f1()93pythono