Re: variables exist

2005-04-17 Thread Peter Otten
Michael J. Fromberger wrote: > 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: > > >>> x = 5 > >>> def f(y): > ... z = 10 > ... print isset('z')

Re: variables exist

2005-04-17 Thread Michael J. Fromberger
In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] (fabian) wrote: > how testing if a variable exists in python as isset in php?? > Would the following be a satisfactory implementation? def isset(varname, lloc = locals()): return varname in lloc or varname in globals() I believe this wor

Re: variables exist

2005-04-12 Thread Steven Bethard
Scott David Daniels wrote: Brian van den Broek wrote: ... STeVe stressed that the try/except solution is only really appropriate for cases where the failure to have the variable defined is quite rare. Beware: C++ and Java have an immense overhead for exceptions. Python has a very lightweight ex

Re: variables exist

2005-04-12 Thread Fredrik Lundh
Scott David Daniels wrote: > >... STeVe stressed that the try/except solution is only really appropriate > > for cases where the failure to have the variable defined is quite rare. > > Beware: C++ and Java have an immense overhead for exceptions. Python > has a very lightweight exception mechanis

Re: variables exist

2005-04-12 Thread Scott David Daniels
Brian van den Broek wrote: ... STeVe stressed that the try/except solution is only really appropriate for cases where the failure to have the variable defined is quite rare. Beware: C++ and Java have an immense overhead for exceptions. Python has a very lightweight exception mechanism. You shou

Re: variables exist

2005-04-12 Thread Brian van den Broek
Richard Brodie said unto the world upon 2005-04-12 04:56: "Brian van den Broek" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] 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 mo

Re: variables exist

2005-04-12 Thread Richard Brodie
"Brian van den Broek" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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 whe

Re: variables exist

2005-04-11 Thread Steven Bethard
Brian van den Broek wrote: Fredrik Lundh said unto the world upon 2005-04-11 10:14: "fabian" wrote: 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 al

Re: variables exist

2005-04-11 Thread Brian van den Broek
Fredrik Lundh said unto the world upon 2005-04-11 10:14: "fabian" wrote: 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

Re: variables exist

2005-04-11 Thread Gerald Klix
try: myVariable except NameError: print "Not bound" else: print "Bound" If you want to distinguish between the local an the global environment: if globals().has_key( "myVariable" ): ... versus if locals().has_key( ". HTH, Gerald fabian schrieb: how testing if a variable exists in python as

Re: variables exist

2005-04-11 Thread Fredrik Lundh
"fabian" wrote: > 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 variabl

variables exist

2005-04-11 Thread fabian
how testing if a variable exists in python as isset in php?? thanks -- http://mail.python.org/mailman/listinfo/python-list