How to tell if function was passed a list or a string?

Edward Elliott nobody at 127.0.0.1
Sat May 20 22:32:32 EDT 2006


Roy Smith wrote:

> Ben Finney <bignose+hates-spam at benfinney.id.au> wrote:
>> Currently there's no good duck-typing way to differentiate
>> strings from other sequences.
> 
> I suppose you could do something like:
> 
> try:
>    foo.isalpha
> except AttributeError:
>    print "foo is not a string"

Another way:

if getattr (foo, 'isalpha', False):
    print 'foo is a string'

Of course now string duck types must have an 'isalpha' and list ones can't,
but that shouldn't matter much.

-- 
Edward Elliott
UC Berkeley School of Law (Boalt Hall)
complangpython at eddeye dot net



More information about the Python-list mailing list