散列Python闭包忽略了__closure__变量

时间:2012-11-27 00:09:48

标签: python closures

我正在使用Python 2.7并注意到它在计算hash()时似乎忽略了闭包变量。这是预期的行为吗?这似乎很奇怪,因为==按照我的预期工作。

def foo(x):
    def bar():
        print x
    return bar

>>> foo(1)
<function bar at 0x2aaaaba0e758>
>>> foo(2)
<function bar at 0x2aaaaba0e848>
>>> foo(1) == foo(2)
False
>>> hash(foo(1))
-9223369104822759804
>>> hash(foo(2))
-9223369104822759804
>>> foo(1).__closure__
(<cell at 0x2aaaaba08130: int object at 0x7a97d8>,)
>>> foo(2).__closure__
(<cell at 0x2aaaaba08168: int object at 0x7a97c0>,)
>>> 

0 个答案:

没有答案