By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,942 Members | 1,788 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,942 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.