Python Full Stack road Day20

Source: Internet
Author: User

first edited October 26, 2017, Thursday

Summary
I. Last Class review
Two. Decorative Device
Three. Adorner supplement
Homework
Summary today

Summary

References: Alex

    1. Decorative Device
    2. Non-parametric adorner
    3. With reference decorator
I. Last Class review
    1. Namespaces and Scopes
      1. Built-in namespaces
      2. Global namespaces
      3. Local name space
      4. Global scope: Built-in namespace, global namespace
      5. Local scope: Local namespace
    2. function nesting and nesting scopes
    3. Function object
    4. Closed Package
Two. Decorative Device
    1. Why use adorners and open closure principles
    2. What is an adorner?
    3. Simple implementation of a non-parametric adorner
import timedef timmer(func):    def wrapper():        start_time = time.time()        func()        stop_time = time.time()        print(‘run time is %s ‘ % (stop_time - start_time))    return wrapper@timmer        #index=timmer(index)def index():    time.sleep(3)    print(‘welcome to oldboy‘)index()        #wrapper()
    1. Non-parametric decorator modifier part1
import  timedef timmer(func):    def foo(*args,**kwargs):        start_time = time.time()        func(*args,**kwargs)        stop_time = time.time()        print(‘run time is %s‘ % (stop_time - start_time))    return foo@timmer    #index = timmer(index)def index(name):    time.sleep(2)    print(‘welconme to my home! %s‘ % name)@timmer    #auth = timmer(auth)def auth(name,passwd):    print(name,passwd)index(‘egon‘)        #foo(‘egon‘)auth(‘liuyang‘,‘123‘)    #foo(‘liuyang‘,‘123‘)
    1. Non-parametric decorator modifier part2
import timedef timmer(func):    def foo(*args,**kwargs):        start_time = time.time()        res = func(*args,**kwargs)        stop_time = time.time()        print(‘run time is %s ‘% (stop_time - start_time))        return res    return foo@timmer        # my_max = timmer(my_max)def my_max(x,y):    res = x if x > y else y    return resres = my_max(2, 3)        #foo(2, 3)print(res)
    1. With reference decorator
def auth2(auth_type):    def auth(func):        def foo(*args,**kwargs):            name = input(‘username: ‘).strip()            passwd = input(‘passwd:‘)            if auth_type == ‘sql‘:                if name == ‘liuyang‘ and passwd == ‘123‘:                    print(‘auth successful‘)                    res = func(*args,**kwargs)                    return res                else:                    print(‘auth error‘)            else:                print(‘还没学会‘)        return foo    return auth@auth2(auth_type = ‘sql‘)        #index = auth(index)def index():    print(‘welcome to index page‘)index()        #foo()
Three. Adorner supplement
    1. Function plus multiple non-parametric adorners
@ccc@bbb@aaadef func():    
    1. Function plus a plurality of parametric adorners
@ccc(‘c‘)@bbb(‘b‘)@aaa(‘a‘)def func():    passfunc=ccc(‘c‘)(bbb(‘b‘)(aaa(‘a‘)(func)))
Homework
    1. No
Summary today
    1. Ready to be sorted

Python Full Stack road Day20

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.