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

Fredrik Lundh fredrik at pythonware.com
Wed Jan 12 00:30:20 CET 2005


Guido van Rossum wrote:

The only thing this intends to break /.../

it breaks classic C types:

import cElementTree x = cElementTree.Element("tag") x <Element 'tag' at 00B4BA30> x.copy <built-in method __copy__ of Element object at 0x00B4BA30> x.copy() <Element 'tag' at 00B4BA68> import copy y = copy.copy(x) Traceback (most recent call last): File "", line 1, in ? File "C:\python24\lib\copy.py", line 93, in copy raise Error("un(shallow)copyable object of type %s" % cls) copy.Error: un(shallow)copyable object of type <type 'Element'> dir(x) ['copy', 'deepcopy', 'append', 'clear', 'find', 'findall', 'findtext', 'get', 'getchildren', 'getiterator', 'insert', 'items', 'keys', 'makeelement', 'set'] dir(type(x)) ['class', 'delattr', 'delitem', 'delslice', 'doc', 'getattribute', 'getitem', 'getslice', 'hash', 'init', 'len', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'setitem', 'setslice', 'str']

(and of course, custom C types is the only case where I've ever used copy; the default behavior has worked just fine for all other cases)

for cElementTree, I've worked around this with an ugly reduce hack, but that doesn't feel right...



More information about the Python-Dev mailing list