Behaviour-based interface/protocol implementation?
Terry Reedy
tjreedy at udel.edu
Tue Jan 25 12:48:15 EST 2011
On 1/25/2011 10:32 AM, Alan Franzoni wrote:
> You're right, I forgot about subclass check. But that's really a
> placebo, because it statically checks the object's *class* for such
> method,
That is exactly the proper check. Instance *methods* are defined on the
class.
> not the actual instance:
Function attributes of instances are not methods. This is especially
true of special methods.
> from collections import Sized
>
> class MyObj(object):
> pass
>
> mo = MyObj()
> mo.__len__ = lambda: 1
>
>
> print isinstance(mo, Sized)
> False
This is correct!
print(len(mo))
TypeError: object of type 'MyObj' has no len()
--
Terry Jan Reedy
More information about the Python-list
mailing list