Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #111127

Re: Making Classes Subclassable

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 <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: Making Classes Subclassable
Date Tue, 5 Jul 2016 08:52:16 -0600
Lines 32
Message-ID <mailman.100.1467730382.2295.python-list@python.org> (permalink)
References <ca1ee22e-9769-4e76-81f3-b9d0dcb55212@googlegroups.com> <87eg79q1i0.fsf@handshake.de> <CALwzidkULYyspVyr3ZWSCrM9qVYZzdU3tOzRntyLxjUQ9aQSew@mail.gmail.com>
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 <ian.g.kelly@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <CALwzidkULYyspVyr3ZWSCrM9qVYZzdU3tOzRntyLxjUQ9aQSew@mail.gmail.com>
X-Mailman-Original-References <ca1ee22e-9769-4e76-81f3-b9d0dcb55212@googlegroups.com> <87eg79q1i0.fsf@handshake.de>
Xref csiph.com comp.lang.python:111127

Show key headers only | View raw


On Mon, Jul 4, 2016 at 1:57 AM, dieter <dieter@handshake.de> wrote:
> Lawrence D’Oliveiro <lawrencedo99@gmail.com> writes:
>
>> Some of the classes in Qahirah, my Cairo binding <https://github.com/ldo/qahirah> I found handy to reuse elsewhere, for example in my binding for Pixman <https://github.com/ldo/python_pixman>. Subclassing is easy, but then you need to ensure that operations inherited from the superclass return instances of the right class. This means that superclass methods must never refer 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.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Making Classes Subclassable Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-02 21:31 -0700
  Re: Making Classes Subclassable dieter <dieter@handshake.de> - 2016-07-04 09:57 +0200
    Re: Making Classes Subclassable Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-04 01:34 -0700
      Re: Making Classes Subclassable Michael Selik <michael.selik@gmail.com> - 2016-07-05 05:26 +0000
        Re: Making Classes Subclassable Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-05 00:08 -0700
      Re: Making Classes Subclassable dieter <dieter@handshake.de> - 2016-07-05 09:55 +0200
      Re: Making Classes Subclassable Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-07-05 18:31 +1000
        Re: Making Classes Subclassable Ian Kelly <ian.g.kelly@gmail.com> - 2016-07-05 08:41 -0600
          Re: Making Classes Subclassable Steven D'Aprano <steve@pearwood.info> - 2016-07-06 01:33 +1000
      Re: Making Classes Subclassable Ian Kelly <ian.g.kelly@gmail.com> - 2016-07-05 09:02 -0600
        Re: Making Classes Subclassable Steven D'Aprano <steve@pearwood.info> - 2016-07-06 01:41 +1000
        Re: Making Classes Subclassable Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-05 19:31 -0700
    Re: Making Classes Subclassable Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-05 20:04 -0700
  Re: Making Classes Subclassable Ian Kelly <ian.g.kelly@gmail.com> - 2016-07-05 08:52 -0600

csiph-web