F
fabian
how testing if a variable exists in python as isset in php??
thanks
thanks
fabian said:how testing if a variable exists in python as isset in php??
try:
variable
except NameError:
print "variable not set"
but that is really lousy Python; better make sure you always assign to
all variables, and use None (or another suitable value) to mark that some
variable has no meaningful content right now.
that is, instead of
write
variable = None
if condition:
variable = 1
...
if variable is None:
print "variable not set"
</F>
Brian said:Fredrik Lundh said unto the world upon 2005-04-11 10:14:
I'm a hobbyist and still learning, but the claim the try/except is
"lousy Python" surprise me a bit. The try/except way seems like an
instance of "Easier to ask Forgiveness than Permission", and my sense
is that in 95% of cases that approach is widely considered more Pythonic
than LBYL (of which the suggested idiom seems reminiscent).
As it happens, most of my code does preset names to None, but reading
this thread made me wonder. Are you suggesting setting to None is
better as it makes explicit that there is nothing there, but this may
change in the future? (This is why I do it.) Or for some other reason?
I'm a hobbyist and still learning, but the claim the try/except is
"lousy Python" surprise me a bit.
I think it wasn't the use of try/except as such. It's more that
if you're the developer you ought to know whether variables
are defined or not. It might be a sign you're using global
variables more often than would be considered good style in
Python.
Brian said:... STeVe stressed that the try/except solution is only really appropriate
for cases where the failure to have the variable defined is quite rare.
Scott said:appropriate
Beware: C++ and Java have an immense overhead for exceptions. Python
has a very lightweight exception mechanism. You should _very_seldom_
choose exceptions or not on the basis of performance without measuring
the actual use; you are sure to be surprised.
Structure your code to be easiest to understand. _If_ it is too slow,
investigate ways to improve its performance. The usual way is to
build better data structures and use better algorithms. Only in the
core of a frequently used inner loop should you even care about the
cost of exceptions.
Scott said:Beware: C++ and Java have an immense overhead for exceptions. Python
has a very lightweight exception mechanism. You should _very_seldom_
choose exceptions or not on the basis of performance without measuring
the actual use; you are sure to be surprised.
how testing if a variable exists in python as isset in php??
Michael said:Would the following be a satisfactory implementation?
def isset(varname, lloc = locals()):
return varname in lloc or varname in globals()
I believe this works as desired:
... z = 10
... print isset('z') ## ==> True
... print isset('y') ## ==> True
... print isset('x') ## ==> True
...
Tests:
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.