[New-bugs-announce] [issue16049] Create abstract base classes by inheritance rather than a direct invocation of __metaclass__

Raymond Hettinger report at bugs.python.org
Wed Sep 26 01:20:31 CEST 2012


New submission from Raymond Hettinger:

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.

----------
components: Library (Lib)
messages: 171323
nosy: rhettinger
priority: normal
severity: normal
status: open
title: Create abstract base classes by inheritance rather than a direct invocation of __metaclass__
versions: Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16049>
_______________________________________


More information about the New-bugs-announce mailing list