In [1]:
def f(a):
'''f funtion'''
return a * 2
In [3]:
f.__name__
Out[3]:
In [4]:
g = f
In [5]:
g.__name__
Out[5]:
In [6]:
g.__doc__
Out[6]:
In [8]:
# 所属模块
g.__module__
Out[8]:
In [9]:
f.__defaults__
In [14]:
def f1(a, b=1, c=[]):
print(a, b, c)
In [15]:
f1.__defaults__
Out[15]:
In [17]:
f1.__defaults__[1].append('abc')
In [19]:
# 默认参数不要使用可变对象
f1(100)
In [20]:
f1.__closure__
In [21]:
def f2():
a = 2
return lambda k: a ** k
In [22]:
g2 = f2()
In [26]:
c = g2.__closure__[0]
In [27]:
f2.__closure__
In [28]:
c.cell_contents
Out[28]:
In [40]:
from functools import wraps, update_wrapper, WRAPPER_ASSIGNMENTS, WRAPPER_UPDATES
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
'''wrapper function'''
print('In wrapper')
# wrapper.__name__ = func.__name__
# update_wrapper(wrapper, func, ('__name__', '__doc__'),('__dict__',))
return wrapper
@decorator
def example():
'''example function'''
print('In example')
print(example.__name__)
print(example.__doc__)
print(WRAPPER_ASSIGNMENTS)
print(WRAPPER_UPDATES)
In [ ]: