x=44
def (func):
????return func()
import foo
def bar():
????x=13
????def hello():
????????return "hello %s" %x
????foo.callf(hello)
得到结果为 hello 13
在编写惰性求值的代码的时候,闭包是一个很好的选择
from urllib import urlopen
????def page (url) :
????????def get() :
????????????
大专栏 Python中的闭包 class="keyword">return urlopen (url).read()
????????return get
>>>python=page("http://www.python.org")
>>>jython=page( "http://www.jython.org")
>>>python
<function get at 0x9735f0>
>>>jython
<function get at 0x9737f0>
>>>pydata=python()
>>>jydata=jython()
page()