One, iterator
An object that can iterate with the next () function, which can be called an iterator
1. Iteration:
Iterable (iterative)
list, tuple, DIC, generator, str, func with yield keyword
Iteration:
Traversing with A For loop
Isinstance (OBJ,CLS)
Iterator (iterator)
ITER () can complete the conversion
#Judging list, tuple ... Can be iterated and use Isinstance (target object, iterable) fromCollectionsImportiterablePrint(Isinstance ([],iterable))Print(Isinstance ((), iterable))Print(Isinstance ("", iterable))Print(Isinstance ({},iterable))defTest ():Print('1') yieldNoneg=Test ()#Print (isinstance (test (), iterable))Next (g) next (Test ())#The ' list ' object is not a iterator list of iterators#next ([+])#Introducing Iterators#The iter () function, which allows the conversion of [],{} to a corresponding iterator fromCollectionsImportIterator#See if ITER ([]) is an iteratorPrint(Isinstance (ITER ([]), Iterator))#view types of ITER ([])Print(Type (ITER ([]) ))Print(Isinstance ({},iterator))Print(Isinstance (ITER ({}), Iterator))Print(Type (ITER ({}) ))Print(Isinstance ((), Iterator))Print(Isinstance ("", Iterator))Print(Isinstance (g,iterator))Print(Isinstance (Test (), Iterator))
Second, closed package
1. Conditions of formation
1. Function Nesting Definitions
def funcout (NUM1)
def funin (num2)
#2. The intrinsic function uses the variables of the external function
result = Num1 + num2
# 3. The return value of the external function is the internal functional name
Return Funin
defFuncount (NUM1):Print("funcout Start") defFunin (num2):Print("funcin Start") Result= Num1 +num2Print("Funcin End") returnresultPrint("Funcout End") returnFunin#Funcin1 Stores the address of the function FuninFuncin1 = Funcount (100)Print(Type (funcin1))#Calling intrinsic functionsRESULT1 = Funcin1 (200)Print(RESULT1)
Resunt2 = funcin1 (1)
Print (RESUNT2)
2. Closure Exercises:
Requirement: Find the distance between two points
X1,y1
X2,y2
Import Math
From Math Import *
(x2-x1) **2 + (y2-y1) **2
To find the power of N of a
Pow (a,n)
To find the square root of X
sqrt (x)
#From Math import pow,sqrt fromMathImport*#encapsulates a function that completes the distance between two pointsdefgetdistance (x1,y1,x2,y2): Dis= sqrt (Pow (x2-x1), 2) +pow ((y2-y1), 2)) returnDisPrint(Getdistance (0,0,10,10))Print(Getdistance (0,0,20,20))#using closures to find the distance between two pointsdefgetdisout (x1,y1):defGetdisin (x2,y2):returnsqrt (POW (x2-x1), 2) +pow ((y2-y1), 2)) returnGetdisin#get a closure that points to an intrinsic functionFuncdis =getdisout (0,0)#call closure to get the distance from point (10,10) from Origin (0,0)Print(Funcdis (10,10))Print(Funcdis (20,20))
3.
Adorner: Essence closure
v1.0
def func1 ()
Print (' Finish function 1 ')
def FUNC2 ()
Print (' Finish function 2 ')
def func3 ()
Print (' Finish function 3 ')
v2.0
Requirement: Add permission verification before using 3 functions
def func1 ()
Print (' Permission validation ')
Print (' Finish function 1 ')
def FUNC2 ()
Print (' Permission validation ')
Print (' Finish function 2 ')
def func3 ()
Print (' Permission validation ')
Print (' Finish function 3 ')
#v3.0 using closures for resolutiondefverification ():Print("Personal Information Access") Print("Permission Validation")#using closures to implementdefFuncout (func):Print("Out start") deffuncin ():#Invoke Permission Validationverification ()#who is the way out? Depending on who the parameters are givenfunc ()Print("Out End") returnFuncin@funcoutdeffunc1 ():Print("using features 1")#Func2 = funcout (FUNC2)@funcoutdefFunc2 ():Print("using features 2") @funcoutdeffunc3 ():Print("using features 3")#encapsulation method, extraction out verification#Special: The closure that is done by a function reference (the letter name) as a parameter#func1 = funcout (func1)#func1 ()func1 () Func2 () func3 ( )
4. Multiple Decorators
@funcout1
@funcout2
def test ()
Return " "
defFUNCOUT1 (func):Print("Start decorating 1") deffuncin ():return "the"+func () +""" returnfuncin#add closures for *defFuncout2 (func):Print("Start decorating 2") deffuncin ():return "*"+func () +"*" returnfuncin#"XXX"#getbookname = funcout (getbookname)@funcout2 @funcout1defgetbookname ():#return "The story of 105 men and 3 women" return "Outlaws"Print(Getbookname ()
5. Decorate the function with parameters
Fixed number of parameters
defFuncout (func):deffuncin (x, y):Print("Funin Start") func (x, y)Print("Funin End") returnFuncin@funcoutdefTest (A, b):Print("A =%d,b =%d"%(b)) test ()
No fixed number of parameters
defFuncout (func):#*args: Indicates that the 0,1,2,3 list is useful, **kwargs:a=1,b=2 defFuncin (*args,**Kwargs):Print("funcin Start") func (*args,**Kwargs)Print("Funcin End") returnFuncin@funcoutdefTest (a,b,c):Print("a=%d,b=%d,c=%d"%(a,b,c))#Test (+/-)@funcoutdefTest1 (A, b):Print("a=%d,b=%d"%(A, B)) Test1 ()
Python Core 2 (iterators, closures, adorners, generators)