Article From:https://www.cnblogs.com/lianyeah/p/9495008.html

One, the essence of function names.

Application of function name:

1.The function name is the memory address of the function.

def func1():
    return

print(func1)

2.Function names can be used as variables.

def func1():
    print('hello')
a = func2
b = a
b()

3.The function name can be used as the return value of a function.

def func1():
    return func2

def func2():
    print('hello')
a = func1
b = a
b()()

4.The function name can be used as a parameter of a function.

def func1(x):
    x()

def func2():
    print('hello')
a = func1(func2)

5.The function name can be used as the element of container class data type.

def func1():
    print('hello-1')
def func2():
    print('hello-2')
def func3():
    print('hello-3')

li = [func1,func2,func3]
for i in li:
    i()

Like the function name above, the first class object.

The first class object (first-class object)finger1.Can be created at runtime.2.Can be used as function parameter or return value.3.An entity that can store variables.

 

Two, globals (), locals ().

1.globals():Returns a dictionary of global variables.

2.locals():Returns the dictionary of the local variable in the current location.

def func1():
    a = 3
    b = 5
    print(globals())
    print(locals())
    def func2():
        a = 10
        b = 20
        print(globals())
        print(locals())
    func2()
func1()
print(globals())
print(locals())

 

Three, closure

Closure:

  The inner function refers to the variables (non-global variables) of the outer function, and the outer function returns the name of the inner function, thus forming a closure

def wraaper():
    name = 'alex'
    def inner():
        print(name)
    print(inner.__closure__)
    inner()
    return inner
wraaper()

 

Closure function:

  When a program runs into a function execution, it opens up a space in memory: a local namespace. If the inner layer of the function becomes a closure, the local namespace does not disappear with the end of the function

from urllib.request import urlopen

def index():
    url = "http://www.xiaohua100.cn/index.html"
    def get():
        return urlopen(url).read()
    return get

xiaohua = index()  # get
content = xiaohua()  # get()
content = xiaohua()  # get()
content = xiaohua()  # get()
content = xiaohua()  # get()
content = xiaohua()  # get()
content = xiaohua()  # get()
content = xiaohua()  # get()
content = xiaohua()  # get()
print(content.decode('utf-8'))

 

Four, iterator

1.Iterable objects

The __iter__ method inside the object is the iterated object.

Iterated objects can satisfy iterated protocols.

Iterated objects: STR, list, dict, tuple, set, range ()

#To determine whether an object is an iterated object:?
#The first method
s1 = 'alex'
s2 = {'name':'alex','sex':'man'}
print('__iter__' in dir(s1))
print('__iter__' in dir(s2))

#Second methods
from collections import Iterable

print(isinstance(s1,Iterable))

 

2.Iterator:

The object contains the __iter__ method and the __next__ method is the iterator.

#Determines whether an object is an iterator.
#First kind
f = open('reg',encoding='utf-8') print('_iter_' in dir(f)) #The handle is an iterator True. print('_next_' in dir(f))   # False print('_iter_' in dir(dict)) #A dictionary is an iterated object, not an iterator True. print('_next_' in dir(dict)) #True

#Second kinds
# Is it an iterator?
from collections import Iterator
print(isinstance('alex',Iterator)) #True

 

3.Iterated object vs iterator

   Iterated objects cannot be valued, and iterators can be valued.

# Iterate object ---> transform into iterator.
lis = [1, 2, 3]  # Iterable objects
# ite1 = lis.__iter__()  # Iterator < list_iterator object at 0x0000027A183BFFD0>
ite1 = iter(lis)  # Iterator < list_iterator object at 0x0000027A183BFFD0>
print(ite1)

#How do iterators take values? Next takes one value at a time.
print(iter1.__next__())
print(iter1.__next__())
print(iter1.__next__())
print(iter1.__next__()) #Get out of the wrong way

4.summary

   1.Iterated objects cannot be valued, and iterators can be valued.

   2.Iterators save much memory.

   3.Iterators will only go to one value at a time.

   4.Iterators are values in one direction, and one way ends.

    Application scenario:

    1.The amount of data is very large.

    2.Value only, no other operation.

 

5. while Cyclic analog for cycle: iterator principle

#Step:
# 1.Converting iterated objects into iterators
# 2.Call the __next__ method to get the value.
# 3.Using exception handling to stop reporting errors

s1 = 'ffshf1jdoiajfkas'
for i in s1:
    print(i)

iter1 = s1.__iter__()

while 1:
    try:
        print(iter1.__next__())
    except StopIteration:
        break

 

Leave a Reply

Your email address will not be published. Required fields are marked *