How to copy class objects?

matthias.oberlaender at daimlerchrysler.com matthias.oberlaender at daimlerchrysler.com
Fri Feb 9 16:05:39 CET 2001


In <960b75$a2e$1 at news.sns-felb.debis.de> 
matthias.oberlaender at daimlerchrysler.com wrote:
> This is the question I'd like to be answered:
> 
> Is there some easy way to copy a class object? 
> 
> Here is the background:
> 
> In order to generate classes from templates I'd prefer the following method 
> over writing additional wrapper function
> 
> class X:
>   
>   sratchdir = "/tmp/"
> 
>   <blabla    (methods using scratchdir)>
> 
> # I want a new class Xnew differing from X in that its instances use a 
> different scratch directory.
> Xnew = copyclass(X)
> # Now set scratchdir to something else
> Xnew.scratchdir = '/home/me/tmp'
> 
> 
> 
Quite a few people have already tried to help me. Thanks!
But it seems it is sowewhat dangerous to give background information, because 
it often distracts people from the real question, which is:

How can I copy a class object?  
=========================

Somebody suggested the copy module. But copy.copy does not apply to classes!

Ok, don't tell me that I don't need such a feature for the toy problem given. 
It's solely for illustration. The real intent is much different. 
I can't accept subclassing, because I don't want instances of X and Xnew have 
any base classes in common. So new.classobj won't work either!  Ok?

-- Matthias





More information about the Python-list mailing list