day 012 summary

Yesterday Review

Vararg

*

* Parameter: receiving extra argument position

* Argument (data type may iteration): iteration data types can be beaten

**

** parameter: a keyword receiving extra argument

** argument: break up the dictionary as keyword arguments mass participation

def f1(*args,**kwargs)

Function object

  1. Quote
  2. Earth element as a container
  3. As a function return value
  4. As a function parameter

Nested functions

def f1():
    def f2():
        pass

f2() # 报错

Namespace and scope

  1. Built-in namespace: built-in method
  2. Global name space: In addition to the built-in global and local is
  3. The local name space: the internal function variables and functions

The order of execution: Built-in - "Global -" local

Search order: start from the current local - "Global -" Built

Global scope and role of local scope is not half dime

A local scope and local scope 2 is not half dime

x = 10
def f1():
    x = 20
    def f2():
        x = 3
    
def f2():
    x = 5

f1()
print(x) # 10

Content Today

Closure function

def f1(url):#f1就叫闭包函数
    def spider():
        print(url)
    rerurn spider# 函数对象
taoao=f1('www.taobao.com')
taobao()
baidu=f1('www.baidu.com')
baidu()

Closure function: the interior of the closure function closure function + variable internal closure function with a function of both the package and then return out through the return value in the form of

The closure function to meet at least nested functions

Decorator

Nature decorator is a function of increased functionality to the function

Need to look at two things when a function decorator to add functionality:

1 without changing the source code of the original function

2. Do not change the original function is called

import time
def index():
    '''被装饰的函数'''
    print('index')
    time.sleep(1)
    
# time_count装饰器:对被装饰函数计时
def time_count(func):
    def wrapper():
        s=time.time
        func()
        e=time.time
        print(e-s)
     return wrapper

index=time_count(index)
index()

Return value

import time
def index():
    '''被装饰的函数'''
    print('index')
    time.sleep(1)
    return 'index'
    
# time_count装饰器:对被装饰函数计时
def time_count(func):
    def wrapper():
        s=time.time
        res=func()
        e=time.time
        print(e-s)
        return res
     return wrapper

index=time_count(index)
res=index()
print(res)

Additional parameters

import time
def index(x,y,z=10):
    '''被装饰的函数'''
    print('index')
    time.sleep(1)
    return 'index'
    
# time_count装饰器:对被装饰函数计时
def time_count(func):
    def wrapper(*args,**kwargs):
        s=time.time
        res=func(*args,**kwargs)
        e=time.time
        print(e-s)
        return res
     return wrapper

index=time_count(index)
res=index(10,20)
print(res)

Decorative template

def deco(func):
    def wrapper(*args,**kwargs):
        '''加功能'''
        res=func(*args,**kwargs)
        return res
    return wrapper

Syntactic sugar

Make the code easier

import time

# time_count装饰器:对被装饰函数计时
def time_count(func):
    def wrapper(*args,**kwargs):
        s=time.time
        res=func(*args,**kwargs)
        e=time.time
        print(e-s)
        return res
     return wrapper
    
@time_count()
def index(x,y,z=10):
    '''被装饰的函数'''
    print('index')
    time.sleep(1)
    return 'index'

Three decorator

As the two decorators, parameters must have a fixed position func, but three decorators lifted this restriction. We can not just use a single parameter of the above three decorators, just need to add a few parameters in three decorator multiple parameters. That decorator to three, actually useless extra layer

Iterator

Iterables: contains a __item__method called iterables

It contains: iterators __item__and __next__named iterator method

Builder: function containing the yield keyword is called the generator

Guess you like

Origin www.cnblogs.com/zqfzqf/p/11575185.html