[issue39805] Copying functions doesn't actually copy them

Steven D'Aprano report at bugs.python.org
Sat Feb 29 22:42:42 EST 2020


New submission from Steven D'Aprano <steve+python at pearwood.info>:

Function objects are mutable, so I expected that a copy of a function should be an actual independent copy. But it isn't.

    py> from copy import copy
    py> a = lambda: 1
    py> b = copy(a)
    py> a is b
    True

This burned me when I modified the copy and the original changed too:

    py> a.attr = 27  # add extra data
    py> b.attr = 42
    py> a.attr
    42


`deepcopy` doesn't copy the function either.

----------
components: Library (Lib)
messages: 363039
nosy: steven.daprano
priority: normal
severity: normal
status: open
title: Copying functions doesn't actually copy them
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39805>
_______________________________________


More information about the Python-bugs-list mailing list