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
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
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
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
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
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
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
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