classification
Title: Create abstract base classes by inheritance rather than a direct invocation of __metaclass__
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: Arfrever, asvetlov, bruno.dupuis, eric.araujo, eric.snow, gvanrossum, jkloth, mark.dickinson, pitrou, python-dev, rhettinger
Priority: normal Keywords: easy, patch

Created on 2012-09-25 23:20 by rhettinger, last changed 2012-12-13 17:14 by asvetlov. This issue is now closed.

Files
File name Uploaded Description Edit
16049.patch bruno.dupuis, 2012-12-07 12:50 add ABC helper class to abc review
Messages (14)
msg171323 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2012-09-25 23:20
Since inheritance is more commonplace and more easily understood than __metaclass__, the abc module would benefit from a simple helper class:

    class ABC(metaclass=ABCMeta):
        pass

From a user's point-of-view, writing an abstract base call becomes simpler and clearer:

    from abc import ABC, abstractmethod

    class Vector(ABC):

        @abstractmethod
        def __iter__(self):
            pass

        def dot(self, other):
            'Compute a dot product'
            return sum(map(operator.mul, self, other))

Notice that this class seems less mysterious because it inherits from ABC rather than using __metaclass__=ABCMeta.

Also note, it has become a reasonably common practice for metaclass writers to put the __metaclass__ assignment in a class and have it get inherited rather than requiring users do the metaclass assignment themselves.
msg171331 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2012-09-26 07:52
+1
msg171501 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-09-28 16:25
Agreed.
msg171658 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-09-30 18:36
It looks slightly better, but it would also violate "there is one obvious way to do it".
msg171668 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2012-09-30 20:07
In practice this is indeed how most users of met a classes do it. E.g.
Django. So, +1.

--Guido van Rossum (sent from Android phone)
On Sep 30, 2012 11:36 AM, "Antoine Pitrou" <report@bugs.python.org> wrote:

>
> Antoine Pitrou added the comment:
>
> It looks slightly better, but it would also violate "there is one obvious
> way to do it".
>
> ----------
> nosy: +gvanrossum, pitrou
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue16049>
> _______________________________________
>
msg176658 - (view) Author: Bruno Dupuis (bruno.dupuis) Date: 2012-11-29 16:02
This solution hides the risk of metaclass conflicts, as the user did not explicitly set the metaclass. IMO, this risk should be clearly told in the Doc.
msg177068 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-12-07 05:49
Bruno: do you want to propose an idea for the doc part?  Or even a full patch for this request?
msg177082 - (view) Author: Bruno Dupuis (bruno.dupuis) Date: 2012-12-07 12:41
Éric, here is a full patch. I hope the doc isn't too confuse. I think we lack a word meaning 'has XXX as metaclass', we should imagine a term for that.
msg177248 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2012-12-10 01:47
+1 The patch looks fine.

Éric do you want to apply it?
msg177406 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2012-12-13 13:09
LGTM
msg177412 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-12-13 16:30
Feel free to commit the patch Andrew.  You may want to document the new ABC class before the ABCMeta, as we expect that subclassing will become the preferred way.
msg177420 - (view) Author: Roundup Robot (python-dev) Date: 2012-12-13 17:09
New changeset 9347869d1066 by Andrew Svetlov in branch 'default':
Issue #16049: add abc.ABC helper class.
http://hg.python.org/cpython/rev/9347869d1066
msg177424 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2012-12-13 17:14
Done. I prefer to leave existing class order in documentation.
In general I agree with Eric that ABC should be before ABCMeta in the doc but now it has formalized as helper for metaclass with very short documentation.

To put ABC first we need to transplate almost all content of ABCMeta doc int ABC.

If somebody want to do it — please create new issue and feel free to make a patch.
msg177425 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2012-12-13 17:14
Thanks, Bruno.
History
Date User Action Args
2012-12-13 17:14:52asvetlovsetstatus: open -> closed
resolution: fixed
messages: + msg177425

stage: resolved
2012-12-13 17:14:26asvetlovsetmessages: + msg177424
2012-12-13 17:09:46python-devsetnosy: + python-dev
messages: + msg177420
2012-12-13 16:30:39eric.araujosetmessages: + msg177412
2012-12-13 13:09:14asvetlovsetmessages: + msg177406
2012-12-10 01:47:10rhettingersetmessages: + msg177248
2012-12-07 12:52:56bruno.dupuissetfiles: - 16049.patch
2012-12-07 12:50:46bruno.dupuissetfiles: + 16049.patch
2012-12-07 12:41:39bruno.dupuissetfiles: + 16049.patch
keywords: + patch
messages: + msg177082
2012-12-07 05:49:57eric.araujosetmessages: + msg177068
2012-12-03 07:56:38Arfreversetnosy: + Arfrever
2012-12-02 23:16:14asvetlovsetnosy: + asvetlov
2012-11-29 16:02:45bruno.dupuissetnosy: + bruno.dupuis
messages: + msg176658
2012-11-13 06:38:55eric.snowsetnosy: + eric.snow
2012-10-02 04:33:54rhettingersetassignee: rhettinger
2012-09-30 20:07:35gvanrossumsetmessages: + msg171668
2012-09-30 18:36:51pitrousetnosy: + pitrou, gvanrossum
messages: + msg171658
2012-09-28 16:25:14eric.araujosetnosy: + eric.araujo
messages: + msg171501
2012-09-26 07:52:23mark.dickinsonsetnosy: + mark.dickinson
messages: + msg171331
2012-09-26 03:11:38rhettingersetkeywords: + easy
type: enhancement
2012-09-26 00:12:58jklothsetnosy: + jkloth
2012-09-25 23:20:10rhettingercreate