How to copy a ClassObject?

Duncan Booth duncan.booth at invalid.invalid
Tue Mar 20 15:43:00 CET 2007

Karlo Lozovina <_karlo_ at _mosor.net_> wrote:

> After this, tmp is a copy of First, and modifying will not
> affect

As Peter said, it's not a copy, its a subclass.

Modifying would affect (although in your example it is 
immutable so you cannot modify it).
Also, rebinding will change the value you see in 
whereas rebinding will hide the copy inherited from (so 
a later del would restore it).

> If my code is somehow mistaken and might not function properly in all 
> cases, please correct me. 

Consider yourself corrected.

You could do what you are attempting with:

   tmp = new.classobj('tmp', First.__bases__, dict(First.__dict__))

which creates a new class named 'tmp' with the same base classes and a copy 
of First's __dict__ except that the __name__ attribute for the new class 
will be set to 'tmp'. The attribute values are still shared between the 
classes (which is significant only if they are mutable), but otherwise they 
won't be sharing state.

I have no idea why you would want to do this, nor even why you would want a   
'name' attribute when Python already gives you '__name__'.

More information about the Python-list mailing list