Daniel Fetchinson wrote:
Python 2.5.1 (r251:54863, Oct 30 2007, 13:45:26)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
x = { }
x[lambda arg: arg] = 5
x[lambda arg: arg]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: <function <lambda> at 0x2aaaaabaab18>
Is this a case of "we are all adults here"? I should only blame myself
for making an unnamed function a dictionary key or should it be
forbidden? Or am I missing something completely?
lambda creates a function. Functions are hashable objects, so they can
be keys in a dict. But each time lambda is used it creates a new unique
function which isn't equal to any other function:
>>> (lambda arg: arg) == (lambda arg: arg)
False
However:
>>> x = {}
>>> f = lambda arg: arg
>>> x[f] = 5
>>> x[f]
5
--
http://mail.python.org/mailman/listinfo/python-list