Quicker way to copy()?

has has.temp2 at virgin.net
Wed Jun 9 11:21:34 EDT 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.
Thanks.



More information about the Python-list mailing list