Hi all
Is there any handy untility for checking if a variable is populated at runtime ?
regards,
KM
--
http://mail.python.org/mailman/listinfo/python-list
km wrote:
Is there any handy untility for checking if a variable is populated at
runtime ?
access it, and catch the NameError:
try:
variable
except NameError:
print not defined
else:
print defined
leaving variables undefined is usually bad style, though;
Fredrik Lundh wrote:
km wrote:
Is there any handy untility for checking if a variable is populated at
runtime ?
access it, and catch the NameError:
try:
variable
except NameError:
In a function when accessing a not-yet-bound local variable you may also
(as
Fredrik Lundh wrote:
(snip)
variable = None
... lots of code that may assign to variable ...
if variable is not None:
print not defined
Shouldn't that be:
if variable is None:
print not defined
?-)
Note to the OP: if None is a valid value for the
Steve Holden wrote:
access it, and catch the NameError:
try:
variable
except NameError:
In a function when accessing a not-yet-bound local variable you may also
(as Fredrik knows but didn't bother to say) see an UnboundLocalError
exception,
Fredrik Lundh wrote:
Steve Holden wrote:
access it, and catch the NameError:
try:
variable
except NameError:
In a function when accessing a not-yet-bound local variable you may also
(as Fredrik knows but didn't bother to say) see an UnboundLocalError
exception,