[Python-ideas] [Wild Idea] Static Ducks

Nick Coghlan ncoghlan at gmail.com
Sun Sep 20 08:20:27 CEST 2009

Bruce Leban wrote:
> but of course that doesn't check that d.quack is a function or even has
> the right kind of signature. And I can write
>     def playWithDuck(d):
>       if not isinstance(d, Duck):
>         raise TypeError("need a Duck")
>       ...
> which has it's own problems (I don't need a Duck; I just need something
> that walks like a duck and quacks like a duck).

Actually, so long as Duck is defined as an Abstract Base Class rather
than an ordinary type, then the above does solve your problem. If
someone wants to declare that their type does indeed walk and quack like
a duck, they can just call Duck.register(my_type).

Regarding static typing with type inference, something to look at would
be the repurposing of the auto keyword in C++0x (soon to be known as
C++1x, since the committee has declared the updated standard won't be
getting released this year).


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list