繁体   English   中英

python中的装饰器与调用函数的函数完全相同吗?

[英]is a decorator in python exactly the same as calling a function on a function?

我以为这样做

@f
def g():
   print 'hello'

与...完全相同

def g():
   print 'hello'
g=f(g)

但是,我有这个代码,它使用contextlib.contextmanager:

@contextlib.contextmanager
def f():
    print 1
    yield
    print 2
with f:
    print 3

它起作用并产生1 3 2

当我试图改变它

def f():
    print 1
    yield
    print 2
f=contextlib.contextmanager(f)
with f:
    print 3

我得到了AttributeError: 'function' object has no attribute '__exit__'

我错过了什么? 在contextlib.contextmanager中是否有一些黑魔法,或者我是否误解了装饰器的工作原理?

是的,装饰器与调用函数和分配返回值完全相同

在这种情况下,错误来自因为您没有调用函数,所以正确的代码将是

def f():
    print 1
    yield
    print 2

f=contextlib.contextmanager(f)
with f():
    print 3

我也不确定你是否测试过代码,因为你给出的装饰器代码会因同样的原因而失败

@contextlib.contextmanager
def f():
    print 1
    yield
    print 2
with f:
    print 3

错误:

    with f:
AttributeError: 'function' object has no attribute '__exit__'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM