【发布时间】:2009-08-10 16:38:03
【问题描述】:
这是来自Richard Jones' Blog的一些代码:
with gui.vertical:
text = gui.label('hello!')
items = gui.selection(['one', 'two', 'three'])
with gui.button('click me!'):
def on_click():
text.value = items.value
text.foreground = red
我的问题是:他到底是怎么做到的?上下文管理器如何访问 with 块内的范围?这是一个尝试解决此问题的基本模板:
from __future__ import with_statement
class button(object):
def __enter__(self):
#do some setup
pass
def __exit__(self, exc_type, exc_value, traceback):
#XXX: how can we find the testing() function?
pass
with button():
def testing():
pass
【问题讨论】:
标签: python scope with-statement contextmanager