Test the existence of a variable?

Peter Hansen peter at engcorp.com
Fri Sep 3 09:43:56 EDT 2004


Bernd Kaiser wrote:

> I was once told, that in Python you do something and live with the 
> consequences.
> 
> try:
>     print var
> except NameError, e:
>     print e

Much easier to live with the consequences if there are none:

try:
     var
except NameError, e:
     print e

In other words, merely evaluate the name and catch the exception,
rather than trying to print it!

-Peter



More information about the Python-list mailing list