3

我以为这样做

@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 中是否有一些特别的黑魔法,或者我误解了装饰器的一般工作方式?

4

1 回答 1

5

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

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

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__'
于 2010-06-19T07:30:10.287 回答