码迷,mamicode.com
首页 > 其他好文 > 详细

函数进阶篇

时间:2019-07-15 19:58:20      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:tar   imm   name   show   UNC   ide   目的   class   cli   

1.1 装饰器

1.1.1 开放封闭原则:

对扩展开放,对修改是封闭

1.1.2 装饰器:

装饰它人的,器指的是任意可调用对象,现在的场景装饰器-》函数,被装饰的对象也是-》函数

1.1.3 原则:

1、不修改被装饰对象的源代码

2、不修改被装饰对象的调用方式

1.1.4 装饰器的目的:

在遵循1,2的前提下为被装饰对象添加上新功能

错误的示范

import time

def index():
    time.sleep(3)
    print(‘welecome to index‘)

def timmer(func):
    start=time.time()
    func()
    stop=time.time()
    print(‘run time is %s‘ %(stop-start))
timmer(index)

1.1.5 正确的示范

import time

def index():
    time.sleep(3)
    print(‘welecome to index‘)

def timmer(func):
    # func=index #最原始的index
    def inner():
        start=time.time()
        func() #最原始的index
        stop=time.time()
        print(‘run time is %s‘ %(stop-start))
    return inner

index=timmer(index) #index=inner
# print(f)
index() #inner()

 

1.2 装饰器的修订

1.2.1 装饰器语法:

在被装饰对象正上方单独一行写上,@装饰器名

@deco1
@deco2
@deco3
def foo():
    pass
foo=deco1(deco2(deco3(foo)))

 

1.2.2 改进一:

import time
def timmer(func):
    def inner():
        start=time.time()
        res=func()
        stop=time.time()
        print(‘run time is %s‘ %(stop-start))
        return res
    return inner

@timmer #index=timmer(index)
def index():
    time.sleep(1)
    print(‘welecome to index‘)
    return 1111

res=index() #res=inner()
print(res)

1.2.3 改进二:

import time
def timmer(func):
    def inner(*args,**kwargs):
        start=time.time()
        res=func(*args,**kwargs)
        stop=time.time()
        print(‘run time is %s‘ %(stop-start))
        return res
    return inner

@timmer #index=timmer(index)
def index(name):
    time.sleep(1)
    print(‘welecome %s to index‘ %name)
    return 1111

res=index(‘egon‘) #res=inner(‘egon‘)
print(res)

@timmer #home=timmer(home)
def home(name):
    print(‘welcome %s to home page‘ %name)

home(‘egon‘) #inner(‘egon‘)

 

1.3 有参装饰器

import time

def auth(func): # func=index
    def inner(*args,**kwargs):
        name=input(name>>: ).strip()
        password=input(password>>: ).strip()
        if name == egon and password == 123:
            print(login successful)
            return func(*args,**kwargs)
        else:
            print(login err)
    return inner

@auth
def index(name):
    time.sleep(1)
    print(welecome %s to index %name)
    return 1111

res=index(egon)
print(res)

#有参装饰器

技术图片
import time

 

def auth2(engine=file):

    def auth(func): # func=index

        def inner(*args,**kwargs):

            if engine == file:

                name=input(name>>: ).strip()

                password=input(password>>: ).strip()

                if name == egon and password == 123:

                    print(login successful)

                    return func(*args,**kwargs)

                else:

                    print(login err)

            elif engine == mysql:

                print(mysql auth)

            elif engine == ldap:

                print(ldap auth)

            else:

                print(engin not exists)

        return inner

    return auth

 

@auth2(engine=mysql) #@auth #index=auth(index) #index=inner

def index(name):

    time.sleep(1)

    print(welecome %s to index %name)

    return 1111

 

res=index(egon) #res=inner(‘egon‘)

print(res)
View Code

1.4 并列多个装饰器

import time

def timmer(func):
    def inner(*args,**kwargs):
        start=time.time()
        res=func(*args,**kwargs)
        stop=time.time()
        print(run time is %s %(stop-start))
        return res
    return inner

def auth2(engine=file):
    def auth(func): # func=index
        def inner(*args,**kwargs):
            if engine == file:
                name=input(name>>: ).strip()
                password=input(password>>: ).strip()
                if name == egon and password == 123:
                    print(login successful)
                    return func(*args,**kwargs)
                else:
                    print(login err)
            elif engine == mysql:
                print(mysql auth)
            elif engine == ldap:
                print(ldap auth)
            else:
                print(engin not exists)
        return inner
    return auth

@auth2(engine=file)
@timmer
def index(name):
    time.sleep(1)
    print(welecome %s to index %name)
    return 1111

res=index(egon)
print(res)

1.5 wraps补充

from functools import wraps
import time

def timmer(func):
    @wraps(func)
    def inner(*args,**kwargs):
        start=time.time()
        res=func(*args,**kwargs)
        stop=time.time()
        print(run time is %s %(stop-start))
        return res
    # inner.__doc__=func.__doc__
    # inner.__name__=func.__name__
    return inner

@timmer
def index(name): #index=inner
    ‘‘‘index 函数。。。。。‘‘‘
    time.sleep(1)
    print(welecome %s to index %name)
    return 1111

# res=index(‘egon‘)
# print(res)

print(help(index))

 

函数进阶篇

标签:tar   imm   name   show   UNC   ide   目的   class   cli   

原文地址:https://www.cnblogs.com/linxidong/p/11190972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!