Specifing arguments type for a function

George Sakkis george.sakkis at gmail.com
Tue Jun 20 07:57:25 EDT 2006


Maric Michaud wrote:

> Le Mardi 20 Juin 2006 13:28, Maric Michaud a écrit :
> > if not getattr(arg, '__iter__') and not getattr(arg, '__getitem__') :
> >     raise ValueError('Function accepts only iterables') # or error handling
> > code
>
> oops, hasattr of course :
>
> if not hasattr(arg, '__iter__') and not hasattr(arg, '__getitem__') :
>      raise ValueError('Function accepts only iterables') # or error handling

This is ok - in theory. In practice I've found that e.g. strings are
more often than not handled as scalars although they are typically
iterables. Also tuples may or may not be considered as iterables,
depending on what they are used for. The definition of scalar is
application-dependent, that's why there is not an isscalar() builtin.

George




More information about the Python-list mailing list