How to copy a ClassObject?

2007-03-20 Thread Karlo Lozovina
Hi all, 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 then 'tmp' becomes just a reference to First, so if I write tmp.name = "Tmp", there goes my First.name. So, how to make 'tmp' a copy of First, I tried using

Re: How to copy a ClassObject?

2007-03-20 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Karlo Lozovina wrote: > 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 > > then 'tmp' becomes just a reference to First, so if I write > tmp.name = "Tmp", there goes my First.name

Re: How to copy a ClassObject?

2007-03-20 Thread Karlo Lozovina
Karlo Lozovina <[EMAIL PROTECTED]> wrote in news:[EMAIL PROTECTED]: > 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

Re: How to copy a ClassObject?

2007-03-20 Thread Peter Otten
Karlo Lozovina wrote: > Yes, I can do a: >   class tmp(First): >   pass > > but I'd rather make a copy than a subclass. > tmp = new.classobj('tmp', (First,), {}) That line creates a subclass just as the simpler approach you gave above. Peter -- http://mail.python.org/mailman/listinfo/py

Re: How to copy a ClassObject?

2007-03-20 Thread Duncan Booth
Karlo Lozovina <[EMAIL PROTECTED]> wrote: > After this, tmp is a copy of First, and modifying tmp.name will not > affect First.name. As Peter said, it's not a copy, its a subclass. Modifying tmp.name would affect First.name (although in your example it is immutable so you cannot modify it). Als

Re: How to copy a ClassObject?

2007-03-20 Thread Stargaming
Karlo Lozovina schrieb: > Karlo Lozovina <[EMAIL PROTECTED]> wrote in > news:[EMAIL PROTECTED]: > > >>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 f

Re: How to copy a ClassObject?

2007-03-20 Thread Peter Otten
Stargaming wrote: > Leave out the `new` module and use `type()` (exactly the same call as to > `new.classobj`) to achieve the same effect. No. new.classobj() creates a classic class whereas type() creates a new-style class. Also, >>> class First: pass ... >>> type("Tmp", (First,), {}) Tracebac

Re: How to copy a ClassObject?

2007-03-21 Thread Karlo Lozovina
Duncan Booth <[EMAIL PROTECTED]> wrote in news:[EMAIL PROTECTED]: > 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 c