Newbie: is a variable defined?

Chad Netzer cnetzer at mail.arc.nasa.gov
Fri Dec 20 17:18:30 EST 2002


On Friday 20 December 2002 12:56, Dennis Lee Bieber wrote:

>         However, be advised that the above does NOT catch a
> variable that has had None assigned to it. For that you need to
> explicitly test for == None.

> if efrom == None:
>         # no from address was found
> else:
>         # found a from address

   The preferred way to test against None is with the "is" keyword, 
presumably because something can equal None without being None 
(from memory, if I'm wrong, someone correct this).  And None is 
unique and special (and maybe, one day, un-rebindable).

So, I always do:

if a is not None:
   print "whatever"

or

if b is None:
   somefunc()

rather than testing "a != None" or "a == None".  A small 
difference, but I seem to recall being bit once by the slight 
difference.

-- 
Bay Area Python Interest Group - http://www.baypiggies.net/

Chad Netzer
cnetzer at mail.arc.nasa.gov




More information about the Python-list mailing list