Built-in functions:
Dir (str) or dir (' Alex ')---> see which methods are built into this type, and return a list
DICT.__ITER__ () or ITER (DICT)---> Convert an Iterative object to an iterator
Iter1.__next__ () or Next (iter1)---> Outputs the element one by one inside the iterator
Globals ()
Locals ()
Introduce a module to determine whether an object is an iterative object, or an iterator
From collections Import Iterator
Print (Isinstance (' alex,iterator))----> Determine if it is an iterator
From collections Import iterable
Print (Isinstance (' Alex ', iterable))---> Determine if it is an iterative object
Skip BUG:
Try: #跳过报错
Pass
Expect Stopiteration
Pass
1.break Breaking for Loop
List1 = [1,2,3]for i in List1: print (i) if i = = 1: Break
2. Understanding of Functions:
Built-in functions
Custom functions: Generally run out of memory, except for the closure function
Python can be very flexible in customizing functions, and functions can be called multiple times
3. Function name (function object) Application scenario: function name points to the memory address of the function, the function name parentheses is the function call
-------> function names are the first class of objects
A. Function name can be assigned as a value to another variable (normally called function)
def func (): PASSF = FUNCF ()
B. Function names can be used as arguments to functions (call another function in a function)
Def func1 (): print (666) def FUNC2 (s): s () Func2 (FUNC1)
C. Function name can be used as return value of function (when closure function)
Def wraaper (): def inner (): print (666) return innerret = Wraaper () ret ()
D. Function names can be used as elements inside the container class type (multiple functions are called at once)
Def func1 (): print (1) def func2 (): print (2) def func3 (): print (3) List1 = [Func1,func2,func3]for i in list1:< C3/>i ()
3. Closure function:
Application: Crawler, decorator
4.globals () and locals ()
Parentheses are called functions: Globals (), locals ()
Parentheses are called attribute global,nonlocal.
Distinguish:
Globals (): Returns only global and built-in variables that form a dictionary
Local (): Returns the current position of the variable, forming a dictionary
Def func1 (): a = 2 b = 3 print (Globals ()) print (Locals ()) def inner (): c = 5 d = 6 Print (Globals ()) print (Locals ()) inner () func1 ()
def func (): a = 3 b = 4 print (Locals ()) print (Globals ()) func () A = 1b = 2print (Globals ()) print (Locals ())
5. Iterative objects and iterators (all objects in Python)
The judgment of an iterative object: The object has a built-in __iter__ method, which is an iterative object
(Str,list,dict,ret,tuple,range ())
Iterator judgment: The object has a built-in __iter__ method, and the __next__ method is the iterator
(Handle F)
1. Manual conversion:
List1 = [1,2,3]iter1 = list1.__iter__ () print (iter1.__next__ ())
2.for Cycle Automatic Conversion
List1 = [1,2,3]for i in List1: print (i)
To simulate the automatic conversion process for a for loop with a while loop
List1 = [1,2,3]iter1 = list1.__iter__ () while 1: try: print (iter1.__next__ ()) except Stopiteration Break
Characteristics of iterators:
1. The iterator creates a memory address in memory that takes only one value at a time: (can handle big data)
An iterative object, however, opens up N addresses at read time, which consumes memory
2. The value of the iterator is one-way, cannot be reversed, can have the function of recording node
Python Learning day 11th, function, closure function, function name, iterate object with iterator Globas () L and locals ()