Behaviour-based interface/protocol implementation?

Daniel Urban urban.dani at gmail.com
Thu Jan 27 17:41:08 EST 2011


On Thu, Jan 27, 2011 at 20:05, Alan Franzoni <mailing at franzoni.eu> wrote:
> On Thu, Jan 27, 2011 at 8:03 PM, Alan Franzoni <mailing at franzoni.eu> wrote:
>> Yes, __instancecheck__ could be used as an alternative hook with
>> respect to maybe_implemented_by(), but there's no such logic for
>> signature checking. That's a minor detail, I think.
>
> On the contrary, now that I double checked, it can't be used that way;
> I don't want to modify the object I'm testing (it could be a third
> party object) nor I want to force it to be "registered" as a certain
> ABC; so __instancecheck__() is just useless here.

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.


Daniel



More information about the Python-list mailing list