>>> @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'
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有