How to copy class objects?

matthias.oberlaender at daimlerchrysler.com matthias.oberlaender at daimlerchrysler.com
Mon Feb 12 07:16:59 EST 2001


Folks, I don't want to be unfriendly. So, thanks for picking up my post and 
the numerous replies. 
But, ...
the subject line reads "How to copy class objects?" 
About 90 % of the replies  don't even refer to that question.
Once again: I just invented examples so we can talk about something concrete. 
Perhaps class copying is not the best thing to do in thoses cases. But that 
is just _another_ question question. 

Ok, yes, I agree that in over 90 percent a feature someone demands does not 
match his real intent. But not in my case. Please, believe it.

Well.,somebody hinted me towards the 'new' module. Thanks a lot. After a 
litle trying by myself it seems that the following code would answer my 
question:

# make a clone of class X, with name XClone, and bind it to name Xclone.
Xclone = new.classobj('Xclone', X.__bases__, X.__dict__.copy())

I suspect that Xclone is indeed a perfect clone of X. But  changes to 
Xclone's attributes would not affect X (ensured by copying X.__dict__). So I 
can even kick out a method! Right?

--
 ____  __  _/_/ . 
( / / ( /  / / /  





More information about the Python-list mailing list