Quicker way to copy()?

H

has

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top