Python闭包
#! /usr/bin/env pythondef closuretesting():
b = 15
c = 20
print 'b id %x'% id(b)
print 'c id %x'% id(c)
def line(x):
return 2*x + b + c
return line
testline=closuretesting()
print(testline.__closure__) # 这个属性中的值,你会发现正好为b 和 c的ID ,因此可以得知,闭包是通过这个属性去记录类似于b,c这样的变量的
print(testline.__closure__.cell_contents)# __closure__里包含了一个元组(tuple),这个元组中的每个元素是cell类型的对象
print(testline.__closure__.cell_contents)
print(testline(10))
页:
[1]