Python 装饰器笔记

一、装饰器无参数

1.原函数无参数

def wrap_in_tag_b(fn): # wrap_in_tag_b 是真正的装饰器
    def wrapped():
        return "<b>" + fn() + "</b>"
    return wrapped

def wrap_in_tag_i(fn):
    def wrapped():
        return "<i>" + fn() + "</i>"
    return wrapped

@wrap_in_tag_b
@wrap_in_tag_i
def hello():
    return "hello"

print(hello()) # returns "<b><i>hello world</i></b>"

 

2.原函数带参数

def wrap_in_tag_b(fn):
    def wrapped(arg): # arg 是原函数参数
        return "<b>" + fn(arg) + "</b>"
    return wrapped

def wrap_in_tag_i(fn):
    def wrapped(arg): # arg 是原函数参数
        return "<i>" + fn(arg) + "</i>"
    return wrapped

@wrap_in_tag_b
@wrap_in_tag_i
def hello(name):
    return "hello {}".formar(name)

print(hello('Jack')) 

 

二、装饰器带参数

1.原函数无参数

def wrap_in_tag(deco_arg):
    # 装饰器参数:deco_arg
    # 可以在任意位置使用
    def real_decorator(func):  # real_decorator才是即将返回的真正的装饰器
    # 原函数参数:func_arg 
    # 只可以在此位置使用

    def wrapped() : return "<{0}>{1}</{0}>".format(deco_arg, func()) return wrapped return real_decorator @wrap_in_tag('b') @wrap_in_tag('i') def hello(): return "hello" print(hello())

 

2.原函数带参数

def wrap_in_tag(deco_arg):
    # 装饰器参数:deco_arg
    # 可以在任意位置使用
    def real_decorator(func):  # real_decorator才是即将返回的真正的装饰器
        def wrapped(func_arg):
            # 原函数参数:func_arg
            # 只可以在此位置使用
            return "<{0}>{1}</{0}>".format(deco_arg, func(func_arg))
        return wrapped
    return real_decorator
    
@wrap_in_tag('b')
@wrap_in_tag('i')
def hello(name):
    return "hello {}".format(name)
    
print(hello('Jack'))

 


三、装饰器类

1.原函数无参数

class wrap_in_tag(object):
    def __init__(self, deco_arg):
        self.tag = deco_arg  # 装饰器参数:deco_arg

    def __call__(self, func):
        def newf(): # 原函数无参数
            return "<{0}>{1}</{0}>".format(self.tag, func())
        return newf

@wrap_in_tag('b')
@wrap_in_tag('i')
def hello():
    return 'hello'
 
print(hello())


2.原函数带参数

class wrap_in_tag(object):
    def __init__(self, deco_arg):
        self.tag = deco_arg # 装饰器参数:deco_arg
        
    def __call__(self, func):
        def newf(func_arg): # 原函数参数: func_arg
            return "<{0}>{1}</{0}>".format(self.tag, func(func_arg))
        return newf

@wrap_in_tag('b')
@wrap_in_tag('i')
def hello(name):
    return "hello {}".format(name)
 
print(hello('Jack'))

 

四、用装饰器装饰类

1.用函数作为装饰器

def wrap_in_tag(deco_arg):
    def real_decorator(func):
        def wrapped(self, func_arg): # 类方法接收第一个参数都是self,这个必须有,而无论func_arg是否有!!!
            return "<{0}>{1}</{0}>".format(deco_arg, func(func_arg))
        return wrapped
    return real_decorator

class foo(object):
    
    @wrap_in_tag('b')
    @wrap_in_tag('i')
    def hello(self, name):
        return 'hello {}".format(name)
    
    @wrap_in_tag('b')
    @wrap_in_tag('i')
    def byebye(self, name):
        return 'byebye {}".format(name)
    
f = foo()
print(f.hello('Jack')


    2.用类作为装饰器

class wrap_in_tag(object):
    def __init__(self, deco_arg):
        self.tag = deco_arg
        
    def __call__(self, func):
        def newf(slf, func_arg): # slf必须!self被占用了,那就给第一个参数另一个名字slf(随便)
            return "<{0}>{1}</{0}>".format(self.tag, func(func_arg))
        return newf
        
        
        
class foo(object):
    
    @wrap_in_tag('b')
    @wrap_in_tag('i')
    def hello(self, name):
        return 'hello {}".format(name)
    
    @wrap_in_tag('b')
    @wrap_in_tag('i')
    def byebye(self, name):
        return 'byebye {}".format(name)

f = foo()
print(f.hello('Jack'))
 

 

posted @ 2016-01-21 04:48  罗兵  阅读(356)  评论(0编辑  收藏  举报