How to copy a ClassObject?

Stargaming stargaming at gmail.com
Tue Mar 20 15:43:38 EDT 2007


Karlo Lozovina schrieb:
> Karlo Lozovina <_karlo_ at _mosor.net_> wrote in
> news:Xns98F98FEF2B9A6mosornet at 161.53.160.64: 
> 
> 
>>how would one make a copy of a class object? Let's say I have:
>>  class First:
>>    name = 'First'
>>
>>And then I write:
>>  tmp = First
> 
> 
> Silly me, posted a question without Googling first ;>. This seems to be 
> the answer to my question:
> 
> import new
> class First:
>     name = 'First'
> tmp = new.classobj('tmp', (First,), {})
> 
> After this, tmp is a copy of First, and modifying tmp.name will not affect 
> First.name.
> 
> P.S.
> If my code is somehow mistaken and might not function properly in all 
> cases, please correct me. 
> 
> Bye,
> klm.
> 
Leave out the `new` module and use `type()` (exactly the same call as to 
`new.classobj`) to achieve the same effect.



More information about the Python-list mailing list