-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
Comments
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. |
+1 |
Agreed. |
It looks slightly better, but it would also violate "there is one obvious way to do it". |
In practice this is indeed how most users of met a classes do it. E.g. --Guido van Rossum (sent from Android phone)
|
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. |
Bruno: do you want to propose an idea for the doc part? Or even a full patch for this request? |
É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. |
+1 The patch looks fine. Éric do you want to apply it? |
LGTM |
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. |
New changeset 9347869d1066 by Andrew Svetlov in branch 'default': |
Done. I prefer to leave existing class order in 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. |
Thanks, Bruno. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: