[Python-Dev] copy confusion (original) (raw)

Fredrik Lundh fredrik at pythonware.com
Tue Jan 11 23:20:59 CET 2005


back in Python 2.1 (and before), an object could define how copy.copy should work simply by definining a copy method. here's the relevant portion:

...
try:
    copierfunction = _copy_dispatch[type(x)]
except KeyError:
    try:
        copier = x.__copy__
    except AttributeError:
        raise error, \
              "un(shallow)copyable object of type %s" % type(x)
    y = copier()
...

I recently discovered that this feature has disappeared in 2.3 and 2.4. in- stead of looking for an instance method, the code now looks at the object's type:

...

cls = type(x)

copier = _copy_dispatch.get(cls)
if copier:
    return copier(x)

copier = getattr(cls, "__copy__", None)
if copier:
    return copier(x)

...

(copy.deepcopy still seems to be able to use deepcopy hooks, though)

is this a bug, or a feature of the revised copy/pickle design? (the code in copy_reg/copy/pickle might be among the more convoluted pieces of python coding that I ever seen... and what's that smiley doing in copy.py?)

and if it's a bug, does the fact that nobody reported this for 2.3 indicate that I'm the only one using this feature? is there a better way to control copying that I should use instead?



More information about the Python-Dev mailing list