Specifing arguments type for a function

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Jun 22 19:45:10 EDT 2006


George Sakkis a écrit :
> 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.
 >>> hasattr('', '__iter__')
False

> Also tuples may or may not be considered as iterables,
> depending on what they are used for. 

 >>> hasattr((), '__setitem__')
False
 >>> hasattr('', '__setitem__')
False

> The definition of scalar is
> application-dependent, that's why there is not an isscalar() builtin.



More information about the Python-list mailing list