Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create abstract base classes by inheritance rather than a direct invocation of __metaclass__ #60253

Closed
rhettinger opened this issue Sep 25, 2012 · 14 comments
Assignees
Labels
easy stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@rhettinger
Copy link
Contributor

BPO 16049
Nosy @gvanrossum, @rhettinger, @mdickinson, @pitrou, @jkloth, @merwok, @asvetlov, @ericsnowcurrently
Files
  • 16049.patch: add ABC helper class to abc
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/rhettinger'
    closed_at = <Date 2012-12-13.17:14:52.228>
    created_at = <Date 2012-09-25.23:20:10.114>
    labels = ['easy', 'type-feature', 'library']
    title = 'Create abstract base classes by inheritance rather than a direct invocation of __metaclass__'
    updated_at = <Date 2012-12-13.17:14:52.226>
    user = 'https://github.com/rhettinger'

    bugs.python.org fields:

    activity = <Date 2012-12-13.17:14:52.226>
    actor = 'asvetlov'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2012-12-13.17:14:52.228>
    closer = 'asvetlov'
    components = ['Library (Lib)']
    creation = <Date 2012-09-25.23:20:10.114>
    creator = 'rhettinger'
    dependencies = []
    files = ['28240']
    hgrepos = []
    issue_num = 16049
    keywords = ['patch', 'easy']
    message_count = 14.0
    messages = ['171323', '171331', '171501', '171658', '171668', '176658', '177068', '177082', '177248', '177406', '177412', '177420', '177424', '177425']
    nosy_count = 11.0
    nosy_names = ['gvanrossum', 'rhettinger', 'mark.dickinson', 'pitrou', 'jkloth', 'eric.araujo', 'Arfrever', 'asvetlov', 'python-dev', 'eric.snow', 'bruno.dupuis']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue16049'
    versions = ['Python 3.4']

    @rhettinger
    Copy link
    Contributor Author

    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.

    @rhettinger rhettinger added stdlib Python modules in the Lib dir easy type-feature A feature request or enhancement labels Sep 25, 2012
    @mdickinson
    Copy link
    Member

    +1

    @merwok
    Copy link
    Member

    merwok commented Sep 28, 2012

    Agreed.

    @pitrou
    Copy link
    Member

    pitrou commented Sep 30, 2012

    It looks slightly better, but it would also violate "there is one obvious way to do it".

    @gvanrossum
    Copy link
    Member

    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\>


    @rhettinger rhettinger self-assigned this Oct 2, 2012
    @brunodupuis
    Copy link
    Mannequin

    brunodupuis mannequin commented Nov 29, 2012

    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.

    @merwok
    Copy link
    Member

    merwok commented Dec 7, 2012

    Bruno: do you want to propose an idea for the doc part? Or even a full patch for this request?

    @brunodupuis
    Copy link
    Mannequin

    brunodupuis mannequin commented Dec 7, 2012

    É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.

    @rhettinger
    Copy link
    Contributor Author

    +1 The patch looks fine.

    Éric do you want to apply it?

    @asvetlov
    Copy link
    Contributor

    LGTM

    @merwok
    Copy link
    Member

    merwok commented Dec 13, 2012

    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.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Dec 13, 2012

    New changeset 9347869d1066 by Andrew Svetlov in branch 'default':
    Issue bpo-16049: add abc.ABC helper class.
    http://hg.python.org/cpython/rev/9347869d1066

    @asvetlov
    Copy link
    Contributor

    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.

    @asvetlov
    Copy link
    Contributor

    Thanks, Bruno.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    easy stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    6 participants