Testing for a Variable

Steve Holden sholden at holdenweb.com
Fri May 4 07:07:33 EDT 2001


"Brian Quinlan" <BrianQ at ActiveState.com> wrote in ...
> Steve wrote:
> > Probably not a good idea, in case var holds something like
> > "system('rm *')".
> > Since you only need to be sure it can be referenced, simply
> > use a reference,
> > as in:
> >
> > try:
> >     var
> > except NameError:
> >     print "You lose"
> >
> > > # Do something
> > > except NameError:
> > > # Do something else
> > >
> > no-need-to-evaluate-what-can-simply-be-referred-to-ly y'rs -- Steve
>
> The question is, of course, can you simply refer to it? You can't pass
> around undefined variables but you can pass strings. And if the variable
is
> defined by your own application, hopefully it won't have unsafe code it
in.
>
So, did you really mean to write

try:
    eval("var")
except:
    someting else

The only advantage I see to that is allowing you to pass the *name* of the
variable, as a string, as an argument to a function. What am I missing?

regards
 Steve





More information about the Python-list mailing list