duck-type-checking?

J Kenneth King james at agentultra.com
Wed Nov 12 10:21:54 EST 2008


Joe Strout <joe at strout.net> writes:

> Let me preface this by saying that I think I "get" the concept of
> duck- 
> typing.
>
> However, I still want to sprinkle my code with assertions that, for
> example, my parameters are what they're supposed to be -- too often I
> mistakenly pass in something I didn't intend, and when that happens, I
> want the code to fail as early as possible, so I have the shortest
> possible path to track down the real bug.  Also, a sufficiently clever
> IDE could use my assertions to know the type of my identifiers, and so
> support me better with autocompletion and method tips.
>
> So I need functions to assert that a given identifier quacks like a
> string, or a number, or a sequence, or a mutable sequence, or a
> certain class, or so on.  (On the class check: I know about
> isinstance, but that's contrary to duck-typing -- what I would want
> that check to do instead is verify that whatever object I have, it has
> the same public (non-underscore) methods as the class I'm claiming.)
>
> Are there any standard methods or idioms for doing that?
>
> Thanks,
> - Joe

I generally use the 'assert' keyword when I'm in a rush otherwise unit
tests generally catch this kind of thing.



More information about the Python-list mailing list