Issue 16049: Create abstract base classes by inheritance rather than a direct invocation of metaclass (original) (raw)

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 2022-04-11 14:57 by admin. 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) (Python triager) 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
2022-04-11 14:57:36 admin set github: 60253
2012-12-13 17:14:52 asvetlov set status: open -> closedresolution: fixedmessages: + stage: resolved
2012-12-13 17:14:26 asvetlov set messages: +
2012-12-13 17:09:46 python-dev set nosy: + python-devmessages: +
2012-12-13 16:30:39 eric.araujo set messages: +
2012-12-13 13:09:14 asvetlov set messages: +
2012-12-10 01:47:10 rhettinger set messages: +
2012-12-07 12:52:56 bruno.dupuis set files: - 16049.patch
2012-12-07 12:50:46 bruno.dupuis set files: + 16049.patch
2012-12-07 12:41:39 bruno.dupuis set files: + 16049.patchkeywords: + patchmessages: +
2012-12-07 05:49:57 eric.araujo set messages: +
2012-12-03 07:56:38 Arfrever set nosy: + Arfrever
2012-12-02 23:16:14 asvetlov set nosy: + asvetlov
2012-11-29 16:02:45 bruno.dupuis set nosy: + bruno.dupuismessages: +
2012-11-13 06:38:55 eric.snow set nosy: + eric.snow
2012-10-02 04:33:54 rhettinger set assignee: rhettinger
2012-09-30 20:07:35 gvanrossum set messages: +
2012-09-30 18:36:51 pitrou set nosy: + pitrou, gvanrossummessages: +
2012-09-28 16:25:14 eric.araujo set nosy: + eric.araujomessages: +
2012-09-26 07:52:23 mark.dickinson set nosy: + mark.dickinsonmessages: +
2012-09-26 03:11:38 rhettinger set keywords: + easytype: enhancement
2012-09-26 00:12:58 jkloth set nosy: + jkloth
2012-09-25 23:20:10 rhettinger create