Built-in functions added, closure

Built-in functions added, closure

A complement of built-in functions

  1. zip (): Zipper method for the object as a parameter can be iterative, the corresponding element of the object packaged into a tuple, and return the contents of these tuples (iterator), if this element is iterables the number of inconsistencies, according to the length of the shortest return

    lst1 = [1,2,3]
    lst2 = ['a','b','c','d']
    lst3 = (11,12,13,14,15)
    for i in zip(lst1,lst2,lst3):
        print(i)
    
    结果:  (1, 'a', 11)
          (2, 'b', 12)
          (3, 'c', 13)
  2. sorted (): Sort

    l1 = [7, 4, 1, 6,]
    print(sorted(l1)) # 形成了新的列表
    # 加key
    lst = [
        {'name': 'xiaohong', 'age': 73},
        {'name': 'xiaoming', 'age': 35},
        {'name': 'xiaoli', 'age': 25},
    ]
    print(sorted(lst,key=lambda x:x['age']))  #按照年龄排序
    print(sorted(lst,key=lambda x:x['age'],reverse=True))  #第三个参数,逆序
  3. filter (): filtration, filter corresponding to the pattern generator expression returns an iterator

    l1 = [56, 67, 12, 34, 78, 90]
    print(i for i in l1 if i > 60)   #[67,78,90]
    print(list(filter(lambda x:x>60,l1)))   #[67,78,90]
  4. map (): corresponds to the cyclic pattern generator expressions

    l1 = [56, 67, 12, 34, 78, 90]
    print(i**2 for i in l1)    #<generator object <genexpr> at 0x000001E97FFFC888>
    print(list(map(lambda x:x**2,l1)))    #[3136, 4489, 144, 1156, 6084, 8100]
  5. reduce (): python3 removed from the built-in functions, into the Module

    That is, the list of members of the tuple accumulated in accordance with established rules

    from functools import reduce
    print(reduce(lambda x,y:x+y,[1,2,3,4,5]))  #15
    """
    第一次: x,y 1,2 求和 3  记录到内存
    第二次: x,y 3,3 求和 6  记录到内齿
    第三次: x,y 6,4 求和 10  记录到内齿
    第四次: x,y 10,5 求和 15  返回
    """

Second, the closure (test sites, the difficulty)

  1. Closure forming conditions:
    • Closure present in the nested function
    • The inner function of the outer layer of non-function global variable references (modified), resulting in a free variable, the variable will not end with the free end of the run function disappear, to ensure the security of data
    • Function returns the name of layer by layer, until it returns to the outermost layer (reference)
  2. Application package closure:
    • Ensure data security
    • The nature of the decorator
# 第一版
lst = []     #lst是全局变量,不安全
def func(price):
    lst.append(price)
    return sum(lst)/len(lst)

# 第二版
def func(price):
    lst = []        #自由变量
    lst.append(price)
    return sum(lst)/len(lst)

# 为了保证数据的安全,闭包
def func():
    lst = []   #自由变量
    def func1(price):
        lst.append(price)
        return sum(lst)/len(lst)
    return func1    #return是给全局复制了一份,得到了func1的函数地址
avg = func()
print(avg(100000))
print(avg(110000))
print(avg(120000))
print(avg(110000))
# 判断一个函数是不是闭包--函数中有没有自由变量
# 函数名.__code__.co_freevars 查看函数的自由变量

Guess you like

Origin www.cnblogs.com/douzi-m/p/11234963.html