Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'assuming': 0.09; 'subject:method': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'jan': 0.12; '2.7': 0.14; 'a(object):': 0.16; 'construct.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hierarchy': 0.16; 'subclass': 0.16; 'subject:class': 0.16; 'wrote:': 0.18; 'implementing': 0.19; 'thu,': 0.19; 'cc:addr:python.org': 0.22; 'fine': 0.24; 'cc:2**0': 0.24; '15,': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'besides': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'work.': 0.31; 'too.': 0.31; '3.x': 0.31; 'context,': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; "i'd": 0.34; 'subject:from': 0.34; 'basic': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'curious': 0.36; 'i.e.': 0.36; 'should': 0.36; 'too': 0.37; 'jason': 0.38; 'sure': 0.39; 'either': 0.39; '2015': 0.84; 'hard.': 0.84; 'to:none': 0.92; 'technique': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=TPMIUOVVREenLQ8RwLIRgmvZeoLJNxlDqk1lZmQ4vbc=; b=Wt/9H4cEHH2XrkQ6ZYXhIKBgjkOZrqqFEqGZ0iKK2ghmGw7w7fI1XEjUIOaMX8Z9Nh QtnU+ckcAZakj2rpPkR29iUptQwslgBP4TovRA8zQElPQNdcCQtbWxpCk+TIpKJ8saNM nhLFlH/JHxztJhHPVbaaQlmPz3xO8cD3j7WhmjSnemVr3HhnCjreexsYU4ZAmIEC9J4V VBy9AUhTxIhmQLoQD5FXKcXRdfPeLjE0YykTPNiqIXA5Ot8Z5KYC9l5TK8IQNMEEcZAb 1XeY94EjbJXcgRVC3RX9aWeDnsU9Oaz3q2b4N+yFmWt4BYGEv6kivgU9ElDdDpwiBaju k4MA== MIME-Version: 1.0 X-Received: by 10.50.79.196 with SMTP id l4mr5453571igx.14.1421254606349; Wed, 14 Jan 2015 08:56:46 -0800 (PST) In-Reply-To: References: Date: Thu, 15 Jan 2015 03:56:46 +1100 Subject: Re: Calling a derived class's constructor from a parent method From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421255034 news.xs4all.nl 2838 [2001:888:2000:d::a6]:58169 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83766 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) > > If I make a B: > > b = B(0) > > I'd like b.foo(1) to return an instance of B. Is there a way to do that besides implementing a construct(self, s) for each part of the hierarchy? I.e. is there a way for the base class to look at self and find out what type to create? > > I'm using Python 2.7.5, but I'm curious what the 3.x answer is too. Assuming you require that every subclass be able to be constructed with that single parameter, it's not too hard. You get a 'self' parameter; its type is the type you want to construct. def foo(self, s): return type(self)(s) That should work just fine on either 2.7 or 3.x. I'm not sure what this function would do in context, but this basic technique should work. ChrisA