鉴于以下设置:
def mapper(f):
def wrapper(items):
for x in items:
yield f(x)
wrapper.__name__ = f.__name__ # This has no effect!
return wrapper
@mapper
def double(x):
return x * 2
Run Code Online (Sandbox Code Playgroud)
装饰器按预期工作:
>>> [x for x in double([1,2,3])]
[2, 4, 6]
Run Code Online (Sandbox Code Playgroud)
然而,它__name__不是double所希望的:
>>> double([1,2]).__name__
"wrapper"
Run Code Online (Sandbox Code Playgroud)
是否可以强制生成器的名称?或者,是否可以在生成器对象中挖掘并检索名称double?