Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83765
| References | <cb109c66-090a-4779-b00e-43d964ae7252@googlegroups.com> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2015-01-14 10:00 -0700 |
| Subject | Re: Calling a derived class's constructor from a parent method |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.17727.1421254860.18130.python-list@python.org> (permalink) |
On Wed, Jan 14, 2015 at 9:45 AM, jason <jasonsewall@gmail.com> 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 use self elsewhere in the method, it
may be cleaner to use a classmethod here:
@classmethod
def foo(cls, s):
return cls(s)
> I'm using Python 2.7.5, but I'm curious what the 3.x answer is too.
The answer is the same in 3.x.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Calling a derived class's constructor from a parent method jason <jasonsewall@gmail.com> - 2015-01-14 08:45 -0800
Re: Calling a derived class's constructor from a parent method Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-14 10:00 -0700
Re: Calling a derived class's constructor from a parent method Chris Angelico <rosuav@gmail.com> - 2015-01-15 03:56 +1100
Re: Calling a derived class's constructor from a parent method Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-01-14 17:05 +0000
Re: Calling a derived class's constructor from a parent method jason <jasonsewall@gmail.com> - 2015-01-14 10:10 -0800
Re: Calling a derived class's constructor from a parent method Dave Angel <davea@davea.name> - 2015-01-14 13:41 -0500
Re: Calling a derived class's constructor from a parent method alister <alister.nospam.ware@ntlworld.com> - 2015-01-14 18:18 +0000
Re: Calling a derived class's constructor from a parent method Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-15 11:40 +1100
Re: Calling a derived class's constructor from a parent method Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-01-15 04:44 +0000
csiph-web