1. List Generator: code example
1 for in range (2)print(a)34 Run the effect as follows:5 D:\python35\python.exe D:/python Training/s14/day4/ list generation. py6 [0, 2, 4, 6, 8, ten , ]78 Process finished with exit code 0
2. High-order function
A variable can point to a function, and the argument of a function can accept a variable, that is, pass a function name as an argument to another function
The name of the inclusion function in the return value
code example:
1defTest ():2Print("int the test") 3 4defTest2 (func):5Print("In the test2") 6Print(func)7func ()8 9test2 (test)10The results are as follows:D:\python35\python.exe D:/python Training/s14/day4/higher-order functions. PY12inchThe test2<function Test at 0x000000000110e268> #这里是test的内存地址14int the testProcess finished with exit code 0
3, the decorative device
Code examples
1 Import Time2 defTimeer (func):3 defwarpper ():4Start_time=time.time ()5 func ()6Stop_time=time.time ()7 Print("The fun Runn time is %s"% (stop_time-start_time))8 returnWarpper9 @timeerTen deftest1 (): OneTime.sleep (3) A Print("In the test1") - - test1 () the The results of the operation are as follows: -D:\python35\python.exe D:/python Training/s14/day4/Adorner. PY - inchThe test1 -The Fun Runn time is3.000171661376953 + -Process finished with exit code 0
Adorner with parameters
1 Import Time2 3 defTimer (func):4 defDeco (*args,**Kwargs):5Start_time=time.time ()6Func (*args,**Kwargs)7Stop_time=time.time ()8 Print("The func Runn time is%s"% (stop_time-start_time))9 returndecoTen One@timer#test1 = Timer (test1) A deftest1 (): -Time.sleep (3) - Print("In the test1") the - @timer - - deftest2 (name,age): + Print("name:%s,age:%s"%(name,age)) - + test1 () ATest2 ("Zhaofan", 23) at The results of the operation are as follows: - -D:\python35\python.exe D:/python Training/s14/day4/Adorner 3.py - inchThe test1 -The Func Runn time is3.000171661376953 -Name:zhaofan,age:23 inThe Func Runn time is0.0 - toProcess finished with exit code 0
The ultimate version of the decorator
1 Import Time2USER,PASSWD ="Zhaofan","123"3 defAuth (auth_type):4 Print("auth func:", Auth_type)5 defOuter_wrapper (func):6 defWrapper (*args,**Kwargs):7 ifauth_type=="Local":8Username = input ("Username:"). Strip ()9Password = input ("Password:"). Strip ()Ten ifuser = = Username andpasswd==Password: One Print("\033[32;1muser has passed authentication\033[0m") Ares = func (*args,**Kwargs) - Print("------after authentication") - returnRes the Else: -Exit"\033[31;1minvalid username or password\033[0m") - elifauth_type=="LDAP": - Print("No LDAP") + returnwrapper - returnOuter_wrapper + A defindex (): at Print("Welcome to Index page") -@auth (auth_type="Local") - defHome (): - Print("Welcome to Home Page") - return "From home" -@auth (auth_type="LDAP") in defBBS (): - Print("Welcome to BBS page") to + index () - Print(Home ()) the BBS () * $ Panax Notoginseng The results of the operation are as follows: -D:\python35\python.exe D:/python Training/s14/day4/Adorner 4.py the Auth func:local + Auth Func:ldap A Welcome to Index page the Username:zhaofan +Password:123 - User has passed authentication $ Welcome to Home Page $------After authentication - fromHome - No LDAP the -Process finished with exit code 0
4, through the list generation, we can create a list directly. However, with memory limitations, the list capacity is certainly limited. Also, creating a list of 1 million elements takes up a lot of storage space, and if we just need to access the first few elements, the vast majority of the space behind it is wasted.
So, if the list element can be calculated according to an algorithm, can we continue to calculate the subsequent elements in the process of the loop? This eliminates the need to create a complete list, which saves a lot of space. In Python, this side loop computes the mechanism, called the generator: Generator.
1A = [x forXinchRange (10)]2 Print(a)3 4g= (x forXinchRange (10))5 Print(g)6 The results of the operation are as follows:7D:\python35\python.exe D:/python Training/s14/day4/Generator. PY8[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]9<generator Object <genexpr> at 0x0000000000b01db0>Ten OneProcess finished with exit code 0
Generator has only one method: __next__ ()
Generator saves the algorithm, each time it is called next(g) , computes g the value of the next element until it is calculated to the last element, no more elements are thrown when the StopIteration error occurs.
1g= (x forXinchRange (10))2 3 forIinchg:4 Print(i)5 6 7 The results of the operation are as follows:8 9D:\python35\python.exe D:/python Training/s14/day4/called by the generator. PYTen 0 One1 A2 -3 -4 the5 -6 -7 -8 +9 - +Process finished with exit code 0
5. for There are several types of data that can be directly acting on the loop
A class is a collection of data types, such as,,, list tuple , and dict set str so on;
One is generator to include the generator and yield the generator function with the band.
These objects, which can be directly applied to for the loop, are called iterative objects: Iterable .
You can use to isinstance() determine whether an object is an Iterable object
an object that can be called by next() a function and continually returns the next value is Iterator called an iterator:.
Generators are Iterator objects, but,, list dict str Though Iterable they are, they are not Iterator .
Any object that can be used for for the loop is a Iterable type;
All objects that can be used for next() functions are Iterator types, which represent a sequence of lazy computations;
Collection data types such as list , dict ,, and str so on are Iterable not Iterator , however, you can iter() get an object from a function Iterator .
6. JSON and Pickle
Decorators, generators, iterators, Json & Pickle Data serialization