Day11
day11 1
1. Application of Function name (first object) 2
1.1. 1, the direct print function name gets the memory address of the function 2
1.2. 2, function name can be assigned a value Operation 2
1.3. 3, function name can be used as parameter of function 2
1.4. 4, the function name can be used as an element of the container class data type 2
1.5. function names can be used as return values for functions 2
2. Closure Pack 2
2.1. inner function A reference to a non-global variable of outer layer function is called closure 3
2.2. judgment is the name of the closure function. __closure__ 3
2.2.1. None returned is not a closure, the cell is returned .... Is the closure 3
2.3. What's the use of closures? 3
2.3.1. when executing a function, if the interpreter determines that the inner closure of this function exists, Python has a mechanism where the temporary namespace of the closure does not disappear as the function finishes executing. 3
3. Adorner 3
3.1. definition and application 4
3.1.1. adorners are 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. 4
3.1.2. Adorner application scenarios: such as inserting logs, performance tests, transaction processing, caching, and so on. 4
3.2. simple to enter and multiply 4
3.2.1. Starter Decorators 4
3.2.2. Syntax sugar 5
3.2.3. Adorner with parameters 5
3.2.4. holding the adorner for all parameters 6
- function name Application (first object)
- Span style= "FONT-FAMILY:CALIBRI; Font-size:12pt ">1, the direct print function name gets the memory address of the function
- 2, the function name can be assigned a value operation
- 3, function name can be a parameter of the function
- Span style= "FONT-FAMILY:CALIBRI; Font-size:12pt ">4, the function name can be an element of the container class data type
-
- Closed Package
- Inner layer function The reference to the non-global variable of the outer layer function is called the closure packet
- The judgment is the name of the closure function. __closure__
- None returned is not a closure, the cell is returned .... It's a closed packet.
- What's the use of closures?
- When executing a function, if the interpreter determines that the inner closure of this function exists, Python has a mechanism where the temporary namespace of the closure does not disappear as the function finishes executing.
- Decorative Device
- Definition and application
- The 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.
- Simple to enter and multiply
- Starter Decorators
Import time
Def func1 ():
Print (' in func1 ')
def timer (func):
def inner ():
Start = Time.time ()
Func ()
Print (Time.time ()-start)
return inner
FUNC1 = Timer (func1)
Func1 ()
Decorator---simple version
- Grammar Sugar
Import time
def timer (func):
def inner ():
Start = Time.time ()
Func ()
Print (Time.time ()-start)
return inner
@timer #==> func1 = Timer (func1)
Def func1 ():
Print (' in func1 ')
Func1 ()
Decorator---grammar sugar
- Adorner with Parameters
def timer (func):
def inner (a):
Start = Time.time ()
Func (a)
Print (Time.time ()-start)
return inner
@timer
Def func1 (a):
Print (a)
FUNC1 (1)
Adorner--an adorner with parameters
- the adorner holding all parameters
Import time
def timer (func):
def inner (*args,**kwargs):
Start = Time.time ()
Re = func (*args,**kwargs)
Print (Time.time ()-start)
return re
return inner
@timer #==> func1 = Timer (func1)
Def func1 (A, B):
Print (' in func1 ')
@timer #==> Func2 = Timer (FUNC2)
Def Func2 (a):
Print (' In Func2 and get a:%s '% (a))
Return ' fun2 over '
Func1 (' aaaaaa ', ' bbbbbb ')
Print (Func2 (' aaaaaa '))
Adorner---Hold all parameters of the adorner
Day11-python Foundation