[Chicago] duck typing to handle a string or iterable of strings

Carl Meyer carl at oddbird.net
Fri May 20 05:34:34 CEST 2011

On 05/19/2011 10:13 PM, Jesse London wrote:
> It sounds like what you really care about is whether or not you were
> handed an iterable. If you don't want to say:
>     def foo(*strings):
>         ...
> then you can reliably duck-type like so:
>     def foo(strings):
>         if not hasattr(strings, '__iter__'):
>             strings = (strings,)
>         ...

Not in Python 3, FWIW. Since strings are an iterable, it was really only
ever an oversight that they lacked __iter__, and that was fixed in Python 3.


More information about the Chicago mailing list