Check if variable is defined

Fredrik Lundh fredrik at pythonware.com
Fri May 18 03:33:06 EDT 2001


Harald Kirsch wrote:
> > that's extremely bad style in Python, but you probably knew that.
>
> If that were true, it would declare a lot of interesting applications
> for Python in particular and script languages in general as `bad
> style' a priori.

if you want to argue, you could at least bother to read my entire
post:

    > (there are exceptions to this rule, e.g. if you're using a module as
    > a configuration file.  in that case, use vars() or getattr() to treat
    > the module as a dictionary

> After execfile()-ing their command file it *is* often necessary to
> test if certain variables where set or not --- of course depending on
> how you defined your little language.

so use execfile/exec to load stuff into a *new* namespace (you
can pass any dictionary as the namespace arguments.  you can
even populate it before calling execfile/exec).

using execfile/exec to load random stuff into the *current* name-
space is bad style.  period.

Cheers /F





More information about the Python-list mailing list