Re: Calling a derived class's constructor from a parent method

2015-01-14 Thread Mark Lawrence
On 15/01/2015 00:40, Steven D'Aprano wrote: jason wrote: class B(A): def __init__(self, s): A.__init__(self, s) Unrelated: It is better to call super than manually call the superclass. Calling A directly means your class is no longer compatible with multiple inheritance.

Re: Calling a derived class's constructor from a parent method

2015-01-14 Thread Steven D'Aprano
jason wrote: > If I have a class hierarchy like so: > > > class A(object): > def __init__(self, s): > self.s = s > def foo(self, s): > return A(s) A.foo is broken, or at least rude. Change it to this: def foo(self, s): return type(self)(s) >

Re: Calling a derived class's constructor from a parent method

2015-01-14 Thread Dave Angel
On 01/14/2015 01:10 PM, jason wrote: On Wednesday, January 14, 2015 at 12:05:55 PM UTC-5, Mark Lawrence wrote: I'm confused, can you please explain what you're trying to achieve rather than how you're trying to achieve it and I'm sure that others will give better answers than I can :) Good c

Re: Calling a derived class's constructor from a parent method

2015-01-14 Thread alister
On Wed, 14 Jan 2015 17:05:27 +, Mark Lawrence wrote: > On 14/01/2015 16:45, jason wrote: >> If I have a class hierarchy like so: >> >> >> class A(object): >>def __init__(self, s): >> self.s = s >>def foo(self, s): >> return A(s) >> >> class B(A): >>

Re: Calling a derived class's constructor from a parent method

2015-01-14 Thread jason
On Wednesday, January 14, 2015 at 12:05:55 PM UTC-5, Mark Lawrence wrote: > I'm confused, can you please explain what you're trying to achieve > rather than how you're trying to achieve it and I'm sure that others > will give better answers than I can :) > Good call. Coming up with a minimal

Re: Calling a derived class's constructor from a parent method

2015-01-14 Thread Mark Lawrence
On 14/01/2015 16:45, jason wrote: If I have a class hierarchy like so: class A(object): def __init__(self, s): self.s = s def foo(self, s): return A(s) class B(A): def __init__(self, s): A.__init__(self, s) If I make a B: b = B(0)

Re: Calling a derived class's constructor from a parent method

2015-01-14 Thread Chris Angelico
On Thu, Jan 15, 2015 at 3:45 AM, jason wrote: > If I have a class hierarchy like so: > > > class A(object): > def __init__(self, s): > self.s = s > def foo(self, s): > return A(s) > > class B(A): > def __init__(self, s): > A.__init__(self, s) >

Re: Calling a derived class's constructor from a parent method

2015-01-14 Thread Ian Kelly
On Wed, Jan 14, 2015 at 9:45 AM, jason wrote: > class A(object): > def __init__(self, s): > self.s = s > def foo(self, s): > return A(s) Instead of explicitly naming the return class here, do this: return self.__class__(s) Alternatively, since you never u

Calling a derived class's constructor from a parent method

2015-01-14 Thread jason
If I have a class hierarchy like so: class A(object): def __init__(self, s): self.s = s def foo(self, s): return A(s) class B(A): def __init__(self, s): A.__init__(self, s) If I make a B: b = B(0) I'd like b.foo(1) to return an instance o