checking one's type

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Fri Jan 5 08:23:36 EST 2007


belinda thom a écrit :
> Hi,
> 
> I've been using the following hack to determine if a type is  acceptable 
> and I suspect there is a better way to do it:
> 
> e.g.
> 
> if type(s) == type("") :
>    print "okay, i'm happy you're a string"

if isinstance(s, basestring):
   print """
There are very few cases where it makes sens to do such a
test anyway...
"""

> If anyone knows a better way, I'm all ears.

A few special cases excepted (ie your function can accept either a 
string or a sequence of strings...), the best thing is usually to not 
check types at all...



More information about the Python-list mailing list