[Python-3000] my take on "typeclasses"

Talin talin at acm.org
Wed May 10 00:53:48 CEST 2006


Oleg Broytmann <phd <at> oper.phd.pp.ru> writes:

> On Tue, May 09, 2006 at 10:31:02PM +0200, tomer filiba wrote:
> > i.e., "is the object addable?" == "hasattr(obj, '__add__')".
> 
>    Even this is meaningless: '1'+1. So duck typing is:

I don't think I agree with this interpretation. Someone earlier
gave the example of an instance in which callable() would return
the 'wrong' answer:

   def __call__( self ):
      throw "This is not callable"

My interpretation of this is that a function can claim all it
wants that its not callable, that doesn't make it so. It *is*
callable, it just happens to throw an exception as a result.

The resolution to this is to not think of duck typing as
being something to do with types, but instead having to
do with capabilities. An object with an __add__ method has
the *potential* to be added, and 99% of the time that's all
we really need to know.

Duck typing isn't about wanting a duck, its about wanting
an object that has waddling and quacking capabilities.

-- Talin




More information about the Python-3000 mailing list