1. Function nesting: In the process of calling a function, other functions are called
def F1 (): x=1 def F2 (): Print ('fromf2') F2 () F1 ()
2. Namespaces and Scopes
A. Namespaces: places where the name and value of a variable are bound
(1) Built-in namespaces: generated when the Python interpreter starts, storing some python built-in names
(2) Global namespace: generated when executing a file, holding the name of a file-level definition.
(3) Local namespace: During execution, if the function is called, the local namespace of the function is generated. Takes effect when the function is called and expires at the end of the call
Load order: Built-in----> global----> Local
Lookup Order: Local-----> Global-----> Built-in
x=0def F1 (): x=1 def F2 (): x=2 def F3 ( ): x=3 print(", x) F3 () F2 () Print('F1 x is ', X ') F1 ()
B. Scope:
(1) Global scope: Global survival, globally valid.
(2) Local scope: Local survival, local effective.
(3) The scope relationship is fixed at the function definition stage, regardless of the function call location
X=1def F1 (): def F2 (): Print(x) return F2def F3 (func): x=2 func () F3 (F1 ())
3. Closure function
(1). functions defined inside a function
(2). Contains references to external scopes, not to global scopes. It can be called a closure function.
Example:
X=1def F1 (): x=11111111111 def F2 (): Print(x) return F2func=F1 () func () F2 is the closure function
=====================================
Usage of eval:
Take the fields contained in the string separately and execute them.
dic='{' name ': ' User01 ', ' Age ': ' ""}'print(type (DIC)) dic= eval (DIC) Print (Type (DIC))
====================================
4. Decorative Device
(1). The adorner follows the principle: a. Do not modify the source code of the decorated object. B. Do not modify the calling method of the called object.
(2). The purpose of the adorner: Add new functionality to other functions.
The # # Adorner must be written directly above the decorated object and is a separate line.
Adorner frame:
def Timer (func): def wrapper (): func () return Wrapper
Add Parameters:
def Timer (func): def Wrapper (*args,**Kwargs): func (*args,**kwargs) return Wrapper
Example:
(no reference)
Import TimedefTimmer (func):defwrapper (): Start=time.time () func () Stop=time.time ()Print('run time is%s'% (stop-start)) returnWrapper@timmer#Index=timmer (Index)defindex (): Time.sleep (3) Print('Welcome to Index')
Example:
(With reference)
Import TimedefTimmer (func):defWrapper (*args,**Kwargs): Start=time.time () Res=func (*args,**Kwargs) Stop=time.time ()Print('run time is%s'% (stop-start)) returnResreturnWrapper@timmer#Index=timmer (Index)defindex (): Time.sleep (3) Print('Welcome to Index') return123@timmer#Home=timmer (Home)defHome (name): Time.sleep (2) Print('Welcome%s to home page'%name)
5. iterators
Iterative objects: All objects under the __iter__ method are iterative objects.
Iterator object: An iterative object executes the built-in __iter__ method, resulting in an iterator object.
6. Generator
7. Ternary expressions, list parsing, generator expressions.
Python fourth day decorator + iterator + generator