Path: csiph.com!feeder.erje.net!2.eu.feeder.erje.net!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Making Classes Subclassable Date: Tue, 5 Jul 2016 08:52:16 -0600 Lines: 32 Message-ID: References: <87eg79q1i0.fsf@handshake.de> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de BRHekW1fMj6fnhHrUe+pRQ4HC1w7csWZxi2Th1aVPbsw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '*args,': 0.07; 'constructor': 0.07; 'inherited': 0.09; 'instance.': 0.09; 'subclasses': 0.09; 'url:github': 0.09; '<>.': 0.11; 'def': 0.13; '(there': 0.16; '2016': 0.16; '@classmethod': 0.16; 'elsewhere,': 0.16; 'means.': 0.16; 'objects;': 0.16; 'overriding': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subclassing': 0.16; 'subject:Classes': 0.16; 'wrote:': 0.16; 'lawrence': 0.22; 'pass': 0.22; 'am,': 0.23; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'example': 0.26; 'message-id:@mail.gmail.com': 0.27; '**kwargs)': 0.29; '**kwargs):': 0.29; 'signatures': 0.29; 'classes': 0.30; 'class.': 0.30; 'operations': 0.31; 'option': 0.31; 'another': 0.32; 'class': 0.33; 'instances': 0.33; 'received:google.com': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'signature': 0.37; 'means': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'some': 0.40; 'more': 0.63; 'different': 0.63; 'binding': 0.66; 'reuse': 0.66; 'jul': 0.72; 'factory:': 0.84; 'indirect': 0.84; 'subject:Making': 0.84; 'to:name:python': 0.84; 'wish.': 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-transfer-encoding; bh=Mhk3DyUtGfyUgp2ES7DE7c/Ols6Yhz0iicW02i0Zq/A=; b=uOQpm7zEyhpqomZLUNkaWdAzQMs0YXQrf0s3+gvs6/prFnEYkvUolBpc9qoOhk3WGJ GalVeFYaFFFN77F2kHotocwTObFCoT6AbZVYKVqxS3inSVI8x7JWG6Q2B9uc5QjcxU0p 3PumpYaeJHR82ZmGTspsnOTxOzpe21KOwd3whtcq/+bSp1tN3MD0XsYkBMahe0MTt/A6 WkcCxrmlWwliMZQ6SjL5DbQe/WjJDJpselTTwVvOuigqvLsUQv+HfMeDR4xoNMz0Cbk6 DxY5mcFGVmZimQUOXvDDoCg6ANsb2LpnAYLTKTdQes6zSbBAPQKchzh5Ctq8bJewPF4D zjTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-transfer-encoding; bh=Mhk3DyUtGfyUgp2ES7DE7c/Ols6Yhz0iicW02i0Zq/A=; b=Vu0otj9Ph6ENfR+mRFESi3wn70/zK5P3SE9jbHrpPk4QNLBtng/FAo/JDzNz+BjSMT UgX3LTyebgexFu6mRpruu7XcobHVfZXVnsBBsd2mK0jnm+hnjvS8O3HUSjrGbR5WAV0a qDfEH8pl61v9q80VrSY8s5t/2S9vYKVVQ7/c7j+L8isXmSJUrHfIMN1ABUpCWrB2Zrwa 6YMjiue+I9sxoCZ/+uDw3cm+36qtXvdVOMTwq30c6EssEUkE33ibGdN5mGtKni5eoTds UvAlfCrRwY0iXBcxnAbRCxh9FMzL33CH5M7DZvfOr8mSxcV35ni1tz1X3jLToHRA/EmY eEhw== X-Gm-Message-State: ALyK8tK8ECTsshe2OV0DkxbUnjdlSpC/drpCVlSPXIno2ByLemty7psVpnnBqeELQzhjE/EWa3Db2fXFsoMV9w== X-Received: by 10.55.99.23 with SMTP id x23mr22039638qkb.93.1467730378911; Tue, 05 Jul 2016 07:52:58 -0700 (PDT) In-Reply-To: <87eg79q1i0.fsf@handshake.de> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <87eg79q1i0.fsf@handshake.de> Xref: csiph.com comp.lang.python:111127 On Mon, Jul 4, 2016 at 1:57 AM, dieter wrote: > Lawrence D=E2=80=99Oliveiro writes: > >> Some of the classes in Qahirah, my Cairo binding I found handy to reuse elsewhere, for example in my binding for P= ixman . Subclassing is easy, but then= you need to ensure that operations inherited from the superclass return in= stances of the right class. This means that superclass methods must never r= efer directly to the class by name for constructing new objects; they need = to obtain the current class by more indirect means. > > --> "type(obj)" or "obj.__class__" (there are small differences) > give you the type/class of "obj". Another option is a classmethod factory: class X: @classmethod def new(cls, *args, **kwargs): return cls(*args, **kwargs) def method(self): return self.new() class Y(X): pass Y().method() will return a new Y instance. There's not a lot of reason to prefer this over type(self)(), but by overriding the classmethod you can have subclasses that use different signatures in the constructor but still support the same signature in the factory for compatibility with the base class. You can also make the classmethod private, if you wish.