>>> @make_bold ... def get_content(): ... return 'hello world' ... >>> get_content() '<b>hello world</b>'
def get_content(): return 'hello world'
>>> id(get_content) 140090200473112 >>> type(get_content) <class 'function'> >>> get_content <function get_content at 0x7f694aa2be18>
>>> func_name = get_content >>> func_name() 'hello world'
>>> def foo(bar): ... print(bar()) ... return bar ... >>> func = foo(get_content) hello world >>> func() 'hello world'
class FuncObj(object): def init(self, name): print('Initialize') self.name= name def call(self): print('Hi', self.name)
>>> fo = FuncObj('python') Initialize >>> fo() Hi python
@make_bold def get_content(): return 'hello world' # 上面的代码等价于下面的 def get_content(): return 'hello world' get_content = make_bold(get_content)
class make_bold(object): def init(self, func): print('Initialize') self.func = func def call(self): print('Call') return '<b>{}</b>'.format(self.func())
>>> @make_bold ... def get_content(): ... return 'hello world' ... Initialize >>> get_content() Call '<b>hello world</b>'
>>> get_content = make_bold(get_content) Initialize # 调用,实际上直接调用的是make_bold构造出来的函数对象 >>> get_content() Call '<b>hello world</b>'
>>> globals() {} >>> def func(): ... pass ... >>> globals() {'func': <function func at 0x10f5baf28>}
class NoName(object): def call(self): pass func = NoName()
def outer(): print('Before def:', locals()) def inner(): pass print('After def:', locals()) return inner
>>> outer() Before def: {} After def: {'inner': <function outer.<locals>.inner at 0x7f0b18fa0048>} <function outer.<locals>.inner at 0x7f0b18fa0048> >>> outer() Before def: {} After def: {'inner': <function outer.<locals>.inner at 0x7f0b18fa00d0>} <function outer.<locals>.inner at 0x7f0b18fa00d0>
def outer(): msg = 'hello world' def inner(): print(msg) return inner
>>> func = outer() >>> func() hello world
def make_bold(func): print('Initialize') def wrapper(): print('Call') return '<b>{}</b>'.format(func()) return wrapper
>>> @make_bold ... def get_content(): ... return 'hello world' ... Initialize >>> get_content() Call '<b>hello world</b>'
>>> @make_header(2) ... def get_content(): ... return 'hello world' ... >>> get_content() '<h2>hello world</h2>'
@make_header(2) def get_content(): return 'hello world' # 等价于 def get_content(): return 'hello world' unnamed_decorator = make_header(2) get_content = unnamed_decorator(get_content)
def make_header(level): print('Create decorator') # 这部分跟通常的装饰器一样,只是wrapper通过闭包访问了变量level def decorator(func): print('Initialize') def wrapper(): print('Call') return '<h{0}>{1}</h{0}>'.format(level, func()) return wrapper # make_header返回装饰器 return decorator
>>> @make_header(2) ... def get_content(): ... return 'hello world' ... Create decorator Initialize >>> get_content() Call '<h2>hello world</h2>'
@make_bold def get_login_tip(name): return 'Welcome back, {}'.format(name)
class make_bold(object): def init(self, func): self.func = func def call(self, name): return '<b>{}</b>'.format(self.func(name))
class make_bold(object): def init(self, func): self.func = func def call(self, *args, **kwargs): return '<b>{}</b>'.format(self.func(*args, **kwargs))
@make_italic @make_bold def get_content(): return 'hello world'
def get_content(): return 'hello world' get_content = make_bold(get_content) # 先装饰离函数定义近的 get_content = make_italic(get_content)
import functools def make_bold(func): @functools.wraps(func) def wrapper(*args, **kwargs): return '<b>{}</b>'.format(func(*args, **kwargs)) return wrapper
>>> @make_bold ... def get_content(): ... '''Return page content''' ... return 'hello world' # 不用functools.wraps的结果 >>> get_content.name 'wrapper' >>> get_content.doc >>> # 用functools.wraps的结果 >>> get_content.name 'get_content' >>> get_content.doc 'Return page content'
实现装饰器时往往不知道调用方会怎么用,所以养成好习惯加上 functools.wraps 吧。
这次是真·完结了,撒花吧~~~
以上就是总结Python中装饰器的使用介绍的详细内容,更多请关注Gxl网其它相关文章!
查看更多关于总结Python中装饰器的使用介绍的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did84830