How do you know whether a variable has been set at a particular place in the code at runtime? This is not always obvious because (1) the variable could be conditionally set, and (2) the variable could be conditionally deleted. I'm looking for something like defined()
in Perl or isset()
in PHP or defined?
in Ruby.
if condition:
a = 42
# is "a" defined here?
if other_condition:
del a
# is "a" defined here?
import
to "source" a "config file" (i.e. a file that only has assignments in it), it may very well be that some variable has not been defined there. Andimport
is sure cheaper/simpler thanConfigParser
, so for me pragmatism wins over beauty here. – Restivonot condition
", however it has been shown time and again (personal experience, at least) that programmers are imperfect and logic flows can in fact be different than expectations. This is especially true in spiral-development, where assumptions change and not all code is audited/refactored. It costs "almost nothing" to set the variable regardless ofcondition
, even if only used whencondition
is met. – Rivesexec
oreval
. However, that would be very insecure in many ways, so I think its worth a simple parser. Anyway, the nearest equivalent to anisset()
would behasattr()
. One could also useglobals().get()
which is capable of returning a default. – Gnosticism