Behaviour-based interface/protocol implementation?

Alan Franzoni mailing at franzoni.eu
Fri Jan 28 05:32:06 EST 2011


On Thu, Jan 27, 2011 at 11:41 PM, Daniel Urban <urban.dani at gmail.com> wrote:

> Actually it can. You don't have to modify the object, just check for
> the desired methods/signature/whatever. See for example the
> implementation of collections.Hashable.__subclasshook__ in _abcoll.py
> and the abc.ABCMeta.__instancecheck__ method.

Mmh, I don't get your examples (subclasshook checks for *classes*, not
instances, and ABCMeta is a metaclass whose instance is an ABC), but
after properly looking at the PEP (__instancecheck__ seems to be
poorly documented in python standard docs) I've seen that yes,
__instancecheck__ on an ABC can actually be used in place of
maybe_implemented_by and hooking into isinstance().

Thanks!


-- 
Alan Franzoni
--
contact me at public@[mysurname].eu



More information about the Python-list mailing list