typeof foo == 'undefined' ???

Paul Prescod paulp at ActiveState.com
Sat Nov 4 00:11:47 CET 2000

Jay Chalfant wrote:
> ...
> if ( typeof foo.bar == 'undefined' )
>     print("I don't have that!")
> Is there an equivalent test in Python?

Here you go:

if not hasattr(foo, "bar"):
   print "I don't have that!

I prefer the Python way. The fact that Javascript returns "undefined"
can hide bugs in your code because you pass around an undefined value
for a while by accident.

 Paul Prescod

More information about the Python-list mailing list