468,301 Members | 1,522 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,301 developers. It's quick & easy.

ABC question: what is the purpose of the register() method?

I was reading PEP 3119 (http://www.python.org/dev/peps/pep-3119/ ) and
have done some experiments using Python 3.0a5. Now I'm somewhat
puzzled about the purpose of the ABCMeta.register() method.

One can use the register() method to register any class as a virtual
subclass of any ABC. For example one can register `int` on `Iterable`
and therefore it is a subclass which is confirmed in the issubclass
check. What is that good for?

This registration might even conflict with the implementation of
__subclasscheck__. So one might expect that register(C) leads to an
acceptance of a class as a subclass but this isnt't true if
__subclasscheck__(C) rejects it. Why isn't __subclasscheck__ not
sufficient?

Example:
--------------

class Interface(metaclass = ABCMeta):
@classmethod
def __subclasscheck__(cls, C):
return cls.__abstractmethods__.issubset(C.__dict__) # some
contract...

class IAppendable(Interface):
@abstractmethod
def append(self, item): pass
>>issubclass(list, IAppendable)
True
>>issubclass(tuple, IAppendable)
False
>>IAppendable.register(int)
issubclass(int, IAppendable)
False
>>import collections.Iterable as Iterable
Iterable.register(int)
issubclass(int, Iterable)
True

Jun 27 '08 #1
1 1959
On Jun 7, 1:37 pm, Kay Schluehr <kay.schlu...@gmx.netwrote:
I was reading PEP 3119 (http://www.python.org/dev/peps/pep-3119/) and
have done some experiments using Python 3.0a5. Now I'm somewhat
puzzled about the purpose of the ABCMeta.register() method.

One can use the register() method to register any class as a virtual
subclass of any ABC. For example one can register `int` on `Iterable`
and therefore it is a subclass which is confirmed in the issubclass
check. What is that good for?
It's mostly good for types created in C, where it's hard to inherit a
class. For example, builtin types register them selves under some ABCs
in collections.
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Matthew Polder | last post: by
4 posts views Thread by TR | last post: by
7 posts views Thread by int main(void) | last post: by
11 posts views Thread by manstey | last post: by
3 posts views Thread by Kevin Walzer | last post: by
reply views Thread by ngxfer001 | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.