duck-type-checking?

Cristina Yenyxe González García the.blue.valkyrie at gmail.com
Wed Nov 12 12:42:21 EST 2008


2008/11/12 Joe Strout <joe at strout.net>:
>
> 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?

You can use hasattr(object, name), with 'name' as the name of the
public method to check. It returns True if the object responds to that
method, False otherwise.

> Thanks,
> - Joe

Hope it helps :)



More information about the Python-list mailing list