Test the existence of a variable?

Peter Hansen peter at engcorp.com
Fri Sep 3 15:43:56 CEST 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:

except NameError, e:
     print e

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


More information about the Python-list mailing list