1

我正在使用 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>,)
>>> 
4

0 回答 0