Why deepcopy doesnt create new references to lambda function?

python

Solution

This applies not just to lambdas, but to functions without state more generally.

>>> def some_function(word): print word
>>> a = [some_function]
>>> a
[<function some_function at 0x1007026e0>]
>>> copy.deepcopy(a)
[<function some_function at 0x1007026e0>]

Because the functions do not store state, deepcopy does not create a new reference for them. An interesting discussion of topics similar to this issue (though not exactly the same issue) is recorded here: http://bugs.python.org/issue1515

Problem

Found this strange in python: ``` class SomeClass(): def __init__(self): pass a = [SomeClass()] b = copy.deepcopy(a) ``` Output: ``` >>> a [<__main__.Some instance at 0x10051b1b8>] >>> b [<__main__.Some instance at 0x10051b092>] ``` This is just as expected- deepcopy created new `SomeClass()` object for b. But if, ``` f = lambda x:x+1 a = [f] b = copy.deepcopy(a) ``` I get: ``` >>> a [<function <lambda> at 0x10056e410>] >>> b [<function <lambda> at 0x10056e410>] ``` Why deepcopy doesnt create a new lambda instance in the second case? does that mean lambda functions are atomic?

Original source