Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'explicitly': 0.05; 'method,': 0.09; 'subject:method': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; '@classmethod': 0.16; 'a(object):': 0.16; 'cleaner': 0.16; 'naming': 0.16; 'subject:class': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'too.': 0.31; '3.x': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'subject:from': 0.34; 'but': 0.35; 'received:google.com': 0.35; '14,': 0.36; 'curious': 0.36; 'jason': 0.38; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'here:': 0.62; '2015': 0.84; '9:45': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=7rXd/+gQTGDrBAA45dORoN6gniS0u4zVwL0sxCyWmhc=; b=CIC6p+9rpiPWAvka2ekyful8z03JDluoApPDR5Je2S7dCjFKCbCYJVCOWozcUd4AqS tDw1h251yod2RM6Hwux4P6S70nrDVEHWDdB1g7ALJ0xU/EUffAKD1DIOrcDTuQR/pYTW REMfLJKF3wudMeGGbXDGRmuY2p2LF/XFq1RG/yE2Wc4xk1vysvQWDl+5PisTLyq3oqDW u3C1BQxEXjE8VwxUF+GFXesOaqSO70fEGbojdcg2PoCy8ObRhCUm8WEbXl3LYuuabMJy 72AI8Q30v8HCcgnuzkO/yFjO1wSG/td5ZFiBh7QOLIHUtkZEMhRywWjL//ntDr+d0+Yr 6sGA== X-Received: by 10.68.65.112 with SMTP id w16mr7271019pbs.114.1421254851322; Wed, 14 Jan 2015 09:00:51 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Wed, 14 Jan 2015 10:00:11 -0700 Subject: Re: Calling a derived class's constructor from a parent method To: Python 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: 21 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421254860 news.xs4all.nl 2948 [2001:888:2000:d::a6]:55504 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83765 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 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.