Re: Does a function like isset() exist in Python?

2005-06-23 Thread Roy Smith
In article <[EMAIL PROTECTED]>,
 Patrick Fitzsimmons <[EMAIL PROTECTED]> wrote:

> Hi,
> 
> I'm sure I should know this, but I can't find it in the manual.
> 
> Is there a function in Python like the function in PHP isset()?  It
> should take a variable name and return True or False depending on
> whether the variable is initialized.
> 
> Thanks for any help,
> Patrick

The straight-forward thing would be to simply access the variable and catch 
any resulting NameError exception that's raised if it's not defined:

try:
   x
   print "x is defined"
except NameError:
   print "no it's not"

Next question, why do you want to do this?  I suspect for most idioms where 
you would something like this, the more pythonic way would be to set the 
variable to None at some point, then test to see if it's still None later 
on:

x = None
while foo:
   if blah:
  x = baz

if x != None:
   print "x was assigned a value"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does a function like isset() exist in Python?

2005-06-23 Thread Steven D'Aprano
On Wed, 22 Jun 2005 23:09:57 -0400, Patrick Fitzsimmons wrote:

> Hi,
> 
> I'm sure I should know this, but I can't find it in the manual.
> 
> Is there a function in Python like the function in PHP isset()?  It
> should take a variable name and return True or False depending on
> whether the variable is initialized.

What would you use such a function for?

The closest thing I can think of is testing whether a particular object
exists. Eg to test whether your program is running under a version of
Python that defines bools, and if not, define your own objects which act
in a similar way, you would say

try:
False
except NameError:
False = 0
True = not False

Note that there is no need to do something with the name "False" in the
try clause -- just use it.


-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does a function like isset() exist in Python?

2005-06-23 Thread Fredrik Lundh
George Sakkis wrote:

> There are no unitialized variables in python; if you try to access an
> undefined  name, a NameError exception is raised:
>
> try:
> print "foo is", foo
> except NameError:
> print "foo is undefined"

note the order of evaluation:

>>> try:
... print "foo is", foo
... except NameError:
... print "foo is undefined"
...
foo is foo is undefined
>>>





-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does a function like isset() exist in Python?

2005-06-22 Thread George Sakkis
"Patrick Fitzsimmons" wrote:

> Hi,
>
> I'm sure I should know this, but I can't find it in the manual.
>
> Is there a function in Python like the function in PHP isset()?  It
> should take a variable name and return True or False depending on
> whether the variable is initialized.
>
> Thanks for any help,
> Patrick

There are no unitialized variables in python; if you try to access an
undefined  name, a NameError exception is raised:

try:
print "foo is", foo
except NameError:
print "foo is undefined"


To undefine a defined name, use del:
>>> foo=None
>>> print foo
None
>>> del foo
>>> print foo
NameError: name 'foo' is not defined


George

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does a function like isset() exist in Python?

2005-06-22 Thread Esben Pedersen
Patrick Fitzsimmons wrote:
> Hi,
> 
> I'm sure I should know this, but I can't find it in the manual.
> 
> Is there a function in Python like the function in PHP isset()?  It
> should take a variable name and return True or False depending on
> whether the variable is initialized.
> 
> Thanks for any help,
> Patrick

try:
   doDomething(myVar)
except NameError:
   print "myVar is not set"

/Esben
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does a function like isset() exist in Python?

2005-06-22 Thread John Roth
I'm not sure what you mean by initialized. If you're asking
if the identifier exists in the namespace, then you can use
hasattr(), or simply try to reference it and catch the exception
if it doesn't exist.

If the identifier exists, it always has a value.

On the other hand, there is a small gotcha on identifiers
in functions/methods where they have to have a value
assigned before you can reference them. If you run into
this at all frequently, you're probably making your methods
too big to be easily understood.

Of course, if you're playing games with the stack and
trying to print out the values of identifiers on the calling
chain on an exception, all bets are off. See the code in
the py.test module (part of the PyPy project) for how
you can do this.

John Roth

"Patrick Fitzsimmons" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
Hi,

I'm sure I should know this, but I can't find it in the manual.

Is there a function in Python like the function in PHP isset()?  It
should take a variable name and return True or False depending on
whether the variable is initialized.

Thanks for any help,
Patrick 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does a function like isset() exist in Python?

2005-06-22 Thread Tim Leslie
On 6/23/05, Patrick Fitzsimmons <[EMAIL PROTECTED]> wrote:
> Hi,
> 
> I'm sure I should know this, but I can't find it in the manual.
> 
> Is there a function in Python like the function in PHP isset()?  It
> should take a variable name and return True or False depending on
> whether the variable is initialized.

There's no direct function to acheive this however you can find out
the answer by looking in locals() and globals() dictionaries. Before
doing this though you should consider why you want to do it. A lot of
this time when this kind of thing comes up the problem can better be
solved by rearranging the code such that you don't need to check
isset() in the first place.

Tim

> 
> Thanks for any help,
> Patrick
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Does a function like isset() exist in Python?

2005-06-22 Thread Patrick Fitzsimmons
Hi,

I'm sure I should know this, but I can't find it in the manual.

Is there a function in Python like the function in PHP isset()?  It
should take a variable name and return True or False depending on
whether the variable is initialized.

Thanks for any help,
Patrick
-- 
http://mail.python.org/mailman/listinfo/python-list