Quicker way to copy()?

has has.temp2 at virgin.net
Wed Jun 9 17:21:34 CEST 2004

I'm wondering if the following code is acceptable for shallow copying
instances of new-style classes:

    class clone(object):
        def __init__(self, origObj):
            self.__dict__ = origObj.__dict__.copy()
            self.__class__ = origObj.__class__

    a = SomeClass()
    aCopy = clone(a)

I've been using the copy module's copy() function till now, but this
is affecting performance in heavy use so I'd really like to optimise
it out. (This is for HTMLTemplate, which does _lots_ of cloning.) The
above 'clone' seems to work okay OMM (MacPython 2.3.3), but I'm not
completely up on the inner workings of Python classes so if anyone can
tell me whether or not I've missed anything it'd be a big help.

More information about the Python-list mailing list