constructing an object from another instance of the same class

Christoph Groth cwg at falma.de
Fri Jun 18 18:58:17 EDT 2010


Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:

> On Fri, 18 Jun 2010 16:30:00 +0200, Christoph Groth wrote:
>
>> If other is of type Base already, just "pass it on".  Otherwise,
>> construct an instance of Base from it.
>> 
>> **************************************************************** import
>> numpy as np
>> 
>> class Base:
>>     def __init__(self, other):
>>         if isinstance(other, type(self)):
>>             self = other
>>             return
>
> This does not do what you think it does. I wonder whether you've
> actually tried it?

Just quickly.  Sorry, I should have written

class Base:
    def __init__(self, other):
        if isinstance(other, type(self)):
            self.a = other.a
            self.b = other.b
            self.c = other.c
            self.d = other.d
            return
        # ...




More information about the Python-list mailing list