I've noticed some strange behavior with calling str(lambda: foo)
in python. If I assign a lambda to two different variables, it will go to two different memory locations, even if the lambda is clearly the same. For example:
>>> a = lambda: 1
>>> b = lambda: 1
>>> str(a)
'<function <lambda> at 0x0000000000AC6730>'
>>> str(b)
'<function <lambda> at 0x0000000000AC66A8>'
OK, so when I create and assign two lambdas, they occupy different memory locations. So far so good. However, if I create a bunch of lambdas and do not assign them, they are always go to the same place, no matter how radically different the lambdas are. For example, if I run this:
>>> print(str(lambda: 1))
<function <lambda> at 0x00000000011B6730>
>>> print(str(lambda: "Hello"))
<function <lambda> at 0x00000000011B6730>
>>> print(str(lambda: str))
<function <lambda> at 0x00000000011B6730>
>>> print(str(lambda: (lambda: (lambda: 1))))
<function <lambda> at 0x00000000011B6730>
As far as I can tell, this behavior is the same regardless of whether I use python 2 or 3. What's causing this strange behavior?