Re: constructing an object from another instance of the same class

2010-06-21 Thread Carl Banks
On Jun 18, 3:55 pm, Christoph Groth wrote: > Bruno Desthuilliers writes: > > Anyway: the simplest solution here is to replace the call to your Base > > class with a call to a factory function. I'd probably go for something > > like (Q&D untested code and other usual warnings) : > > > (...) > > Ye

Re: constructing an object from another instance of the same class

2010-06-21 Thread Jean-Michel Pichavant
Christoph Groth wrote: Dear all, sometimes it is handy to have a function which can take as argument anything which can be converted into something, e.g. def foo(arg): arg = float(arg) # ... I would like to mimic this behavior of float for a user-defined type, e.g. def bar(arg):

Re: constructing an object from another instance of the same class

2010-06-18 Thread Christoph Groth
Steven D'Aprano 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 >> >>

Re: constructing an object from another instance of the same class

2010-06-18 Thread Christoph Groth
Bruno Desthuilliers writes: > Anyway: the simplest solution here is to replace the call to your Base > class with a call to a factory function. I'd probably go for something > like (Q&D untested code and other usual warnings) : > > (...) Yeah, that will do what I want. My confusion arose from t

Re: constructing an object from another instance of the same class

2010-06-18 Thread Steven D'Aprano
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

Re: constructing an object from another instance of the same class

2010-06-18 Thread Stephen Hansen
On 6/18/10 3:51 AM, Christoph Groth wrote: > sometimes it is handy to have a function which can take as argument > anything which can be converted into something, e.g. [snip] > I would like to mimic this behavior of float for a user-defined type, > e.g. [snip] > Now I wonder what is the most pytho

Re: constructing an object from another instance of the same class

2010-06-18 Thread Bruno Desthuilliers
Bruno Desthuilliers a écrit : Christoph Groth a écrit : Bruno Desthuilliers writes: (snip) In C++ Forget about C++ - Python is a different beast !-) Still, it is useful and interesting to compare languages. Indeed. But you have to understand enough of a language to compare it with ano

Re: constructing an object from another instance of the same class

2010-06-18 Thread Bruno Desthuilliers
Christoph Groth a écrit : Bruno Desthuilliers writes: It seems to me that in this way I might get problems when I pass an instance of Derived_from_my_type to bar, as it will become an instance of My_type. The instance you pass to bar won't "become" anything else. You create a new My_type inst

Re: constructing an object from another instance of the same class

2010-06-18 Thread Christoph Groth
Bruno Desthuilliers writes: >> It seems to me that in this way I might get problems when I pass an >> instance of Derived_from_my_type to bar, as it will become an >> instance of My_type. > > The instance you pass to bar won't "become" anything else. You create > a new My_type instance from the D

Re: constructing an object from another instance of the same class

2010-06-18 Thread Bruno Desthuilliers
Christoph Groth a écrit : Dear all, sometimes it is handy to have a function which can take as argument anything which can be converted into something, e.g. def foo(arg): arg = float(arg) # ... I would like to mimic this behavior of float for a user-defined type, e.g. def bar(arg):

constructing an object from another instance of the same class

2010-06-18 Thread Christoph Groth
Dear all, sometimes it is handy to have a function which can take as argument anything which can be converted into something, e.g. def foo(arg): arg = float(arg) # ... I would like to mimic this behavior of float for a user-defined type, e.g. def bar(arg): arg = My_type(arg) #