Behaviour-based interface/protocol implementation?
Daniel Urban
urban.dani at gmail.com
Fri Jan 28 06:00:58 EST 2011
On Fri, Jan 28, 2011 at 11:32, Alan Franzoni <mailing at franzoni.eu> wrote:
> 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().
Exactly. I mentioned subclasshook because it is possible to define a
similar "instancehook", which would check for instances.
Daniel
More information about the Python-list
mailing list