Python 装饰器原理和基本实现

Python 装饰器原理和基本实现

http://www.cnblogs.com/wupeiqi/articles/4980620.html

http://www.cnblogs.com/vamei

 

装饰器(decorator)是一种高级Python语法。装饰器可以对一个函数、方法或者类进行加工。在Python中,我们有多种方法对函数和类进行加工,比如在Python闭包中,我们见到函数对象作为某一个函数的返回结果。相对于其它方式,装饰器语法简单,代码可读性高。因此,装饰器在Python项目中有广泛的应用。

装饰器是设计模式的一种,被用于有切面需求的场景,较为经典的有插入日志、性能测试、事务处理等。

无参数

def login(func): #func = tv
    print("passed user verification.....")
    #func()
    return func  #return tv的内存地方
def home(name):
    print("Welcome [%s] to home page!" %name)
def tv():
    print("Welcome [%s] to TV page!")
def movie(name):
    print("Welcome [%s] to movie page!" %name)
tv = login(tv)
tv()


/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/admin/PycharmProjects/s18/day3/装饰器.py
passed user verification.....
Welcome [%s] to TV page!

进程已结束,退出代码0


def login(func): #func = tv
    print("passed user verification.....")
    #func()
    return func #tv 的内存地址
def home(name):
    print("Welcome [%s] to home page!" %name)
@login
def tv(name):
    print("Welcome [%s] to TV page!"%name )
def movie(name):
    print("Welcome [%s] to movie page!" %name)


tv('wwww')
# tv = login(tv)
# tv('wwww')

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/admin/PycharmProjects/s18/day3/装饰器.py
passed user verification.....
Welcome [wwww] to TV page!

进程已结束,退出代码0

@login
tv = login(tv)

一个参数

def login(func): #func = tv
    def inner(arg):
        print("passed user verification.....")
        #func()
        return func(arg) #tv 的内存地址
    return inner
def home(name):
    print("Welcome [%s] to home page!" %name)
# @login
def tv(name):
    print("Welcome [%s] to TV page!"%name )
def movie(name):
    print("Welcome [%s] to movie page!" %name)

# tv('wwww')
tv = login(tv)
tv('wwww')

多参数

def login(func): #func = tv
    def inner(*args,**kwargs):
        print("passed user verification.....")
        #func()
        return func(*args,**kwargs) #tv 的内存地址
    return inner
def home(name):
    print("Welcome [%s] to home page!" %name)
@login
def tv(name,passwd):
    print("Welcome [%s] to TV page!"%name )
@login
def movie(name):
    print("Welcome [%s] to movie page!" %name)

# # tv('wwww')
# tv = login(tv)
tv('wwww',passwd='123')
movie('wwwwwwwww')

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/admin/PycharmProjects/s18/day3/装饰器.py
passed user verification.....
Welcome [wwww] to TV page!
passed user verification.....
Welcome [wwwwwwwww] to movie page!

进程已结束,退出代码0

简例

http://www.cnblogs.com/wupeiqi/articles/4980620.html
def w1(func):
    def inner(arg):
        # 验证1
        # 验证2
        # 验证3
        return func(arg)
    return inner

@w1
def f1(arg):
    print ('f1')
print('-------1--------')
f1('1111')


def w1(func):
    def inner(arg1,arg2):
        # 验证1
        # 验证2
        # 验证3
        return func(arg1,arg2)
    return inner

@w1
def f1(arg1,arg2):
    print ('f1')

print('-------2--------')
f1('2222','22')


def w1(func):
    def inner(arg1,arg2,arg3):
        # 验证1
        # 验证2
        # 验证3
        return func(arg1,arg2,arg3)
    return inner

@w1
def f1(arg1,arg2,arg3):
    print ('f1')

print('-------3--------')
f1('3333','33','333')

def w1(func):
    def inner(*args,**kwargs):
        # 验证1
        # 验证2
        # 验证3
        return func(*args,**kwargs)
    return inner

@w1
def f1(arg1,arg2,arg3,arg4):
    print ('f1')
print('-------4--------')
f1('4','44','444','4444')


def w1(func):
    def inner(*args,**kwargs):
        # 验证1
        # 验证2
        # 验证3
        return func(*args,**kwargs)
    return inner

def w2(func):
    def inner(*args,**kwargs):
        # 验证1
        # 验证2
        # 验证3
        return func(*args,**kwargs)
    return inner


@w1
@w2
def f1(arg1,arg2,arg3):
    print ('f1')
print('-------5--------')
f1('5','55','555')

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/admin/PycharmProjects/s18/day3/naozi.py
-------1--------
f1
-------2--------
f1
-------3--------
f1
-------4--------
f1
-------5--------
f1

 

posted on 2017-08-31 10:54  光阴8023  阅读(184)  评论(0编辑  收藏  举报