What is the name of the function?
1, the function name is the memory address of the function
A = 2
b = A
c = b
Print (c)
2, the function name can be used as a variable.
Def func1 ():
Print (666)
F1 = Func1 ()
F2 = F1
Print (F2)
3, the function name can be used as a function parameter.
Def func1 ():
Print (666)
def func2 (x):
Print (x)
X ()
Print (FUNC1)
The function name can be used as the return value of the function.
Def wapper ():
def inner ():
Print (' inner ')
return inner
ret = Wapper ()
RET ()
Globals () returns a dictionary of global variables
Locals () returns the dictionary of local variables for the current position.
Def func1 ():
A = 2
b = 3
Print (Globals ())
Print (Locals ())
def inner ():
c = 6
D = 5
Print (Globals ())
Print (Locals ())
Inner ()
Print (Globals ())
Print (Locals ())
Can iterate over objects
For i in ' abc ':
Print (i)
A __iter__ method inside an object is an iterative object.
An iterative object satisfies an iterative protocol.
An iterative object is: str list dict tuple set range
S1 = ' STRs '
Print (dir (S1))
There are two ways to determine whether an object can iterate over an object:
The first method:
S1 = ' STRs '
DIC = {' name ': ' Alex '}
Print (' __iter__ ' in Dir (S1))
Print (' __iter__ ' in Dir (DIC))
The second method:
From collections Import iterable #Iterable: Can iterate
From collections import Iterator #Iterator: iterators
Print (Isinstance (' Alex ', iterable)) #isinstance: Judging data types and functions, etc...
Print (Isinstance (' Alex ', Iterator))
Print (Isinstance (' Alex ', str))
What is an iterator?
The object contains the __iter__ method and the __next__ method is an iterator.
F=open (' Register.txt ', encoding= ' utf-8 ')
Print (' __iter__ ' in Dir (f))
Print (' __next__ ' in Dir (f))
Print (' __iter__ ' in Dir (dict))
Print (' __next__ ' in Dir (dict))
Iterative Object vs Iterator
An iterator can not take a value.
Iterate Object---> (convert to) iterator
Lis = [+ +]
Itel = lis.__iter__ ()
Itel = iter (LIS)
Print (Itel)
How do iterators take values? Next time, take a value
Print (itel.__next__ ())
Print (itel.__next__ ())
Print (itel.__next__ ())
1, can iterate the object cannot take the value, the iterator can take the value.
2, the iterator is very memory-saving.
3, the iterator will only take one value at a time.
4, the iterator is one-way, a road to the head.
S1 = ' Aqwdew '
1. Convert an iterative object into an iterator.
2, call the __next__ method to take the value.
3, the use of abnormal processing to stop the error.
Iter1 = s1.__iter__ ()
While 1:
Try
Print (iter1.__next__ ())
Except stopiteration:
Break
Python function name, iterative objects and iterators