There is no nonlocal in Python2. What is the name of the function? The function name is the name of the function, the essence: The variable, the special variable. 1. Print function name individually:
def func (): Print (666) Print (func) # <function func at the location where the 0x0000020c8c7c8f28> function is stored.
2. Assignment of the function name:
def func (): Print (666= funcprint(f) # <function func at 0x0000015710a08f28>
3. Function names can be used as parameters:
A = 1def F (x): Print(x) f (a) # 1
4. The function name can be used as an element of the container class data.
deffunc1 ():Print(1111)defFunc2 ():Print(2222)deffunc3 ():Print(3333)#func1 ()#Func2 ()#func3 ()L =[func1,func2,func3] forIinchl:i ()#1111 #2222 #3333deffunc1 ():Print(1111)defFunc2 ():Print(2222)deffunc3 ():Print(3333) L= [] forIinchRange (1,4): L.append ('func'+str (i)) forIinchL:eval (i) ()#What type of eval (i) is inside I can be put forward directly.
5. The function name can be used as the return value of the function.
def func (x): return = func (5)print(ret) # 5
Closure: The inner layer function is a reference to the outer layer function (non-global) variable.
def wraaper (): def inner (): Print (666) return = Wraaper () # innerret () # inner ()
How to tell if it is a closure: the inner function name. __closure__ If a cell is a closure.
def wrapper (): ' old boy ' def inner (): Print (name) inner () print(inner. __closure__) wrapper ()# old boy # (<cell at 0x000001473f9a0b58 : str object at 0x000001474033a5d0>,)
Print None if it is not a closure.
' Gu Qingqiu ' def wraaper (): def inner (): Print (name) inner () print(inner. __closure__) wraaper ()# Gu Qingqiu # None
Interview question: ' Closure ' closure: When the function starts executing, if a closure is encountered, he has a mechanism that will always open up a memory space to put the variables in the closure in the equivalent, and will not disappear as the function executes.
' old boy ' def wraaper (n): # equals n = ' old boy ' def inner (): Print (n) # old boy inner () print(inner. __closure__) # (<cell at 0x0000017b8ccd0b58:str object at 0x0000017b8d63a5d0>,) Wraaper (name)
The first class of objects (first-class object) refers to
1. Can be created at run time.
2. Use as function parameter or return value.
3. Entities that can be stored in variables.
Closures:closure function:
An intrinsic function contains a reference to an outer scope rather than a global scope variable, which becomes a closure function.
The functions defined inside the function are called intrinsic functions.
The closure function gets the network application:
from urllib.request import Urlopen def index ( ): url = http://www.xiaohua100.cn/ index.html " def get (): return urlopen (URL). read (). Decode (" utf-8 " " return Getxiaohua = index () content = Xiaohua () print (content)
What is an adorner?
An adorner is essentially a Python function that allows other functions to add additional functionality without any code changes, and the return value of the adorner is also a function object.
Application scenarios for adorners: such as inserting logs, performance tests, transaction processing, caching, and so on.
the process of forming an adorner:
Decorator---simple version
Import Timedeffunc1 ():Print('In func1')defTimer (func):#func = func1 definner (): Start=time.time () func () Time.sleep (0.3) Print(Time.time ()-start)returninnerfunc1= Timer (func1)#InnerFunc1 ()#Inner ()
Decorator---grammar sugar
Import TimedefTimer (func):definner (): Start_time=time.time () func () Time.sleep (0.3) End_time=time.time ()Print(end_time-start_time)returnInner@timer#syntax Sugar # func1 = Timer (func1)deffunc1 ():Print("In func1") func1 ()
Adorner with parameters:
Import Time def Timer (func): def Inner (a): = Time.time () func (a) time.sleep (0.3) = time.time () return inner@timerdef Func1 (a): print(a) # 1func1 (1)
The adorner holding all parameters
Import TimedefTimer (func):defInner (*args,**Kwargs): Start_time=time.time () ret= Func (*args,**Kwargs) Time.sleep (0.3) End_time=time.time ()Print(end_time-start_time)returnretreturnInner@timerdefFunc1 (A, b):Print(A, b) @timerdefFunc2 (a,b,c):Print(a,b,c)returnTruefunc1 ('AAA','BBB')Print(Func2 ('AAA','BBB','CCC'))
Python ' s Eleventh day for me