473,396 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Does a function like isset() exist in Python?

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
Jul 19 '05 #1
6 47315
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" <pa*****@gmail.com> wrote in message
news:ma**************************************@pyth on.org...
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

Jul 19 '05 #2
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
Jul 19 '05 #3
"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

Jul 19 '05 #4
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


</F>

Jul 19 '05 #5
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.

Jul 19 '05 #6
In article <ma**************************************@python.o rg>,
Patrick Fitzsimmons <pa*****@gmail.com> 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"
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: christian9997 | last post by:
Hi I don't seem to understand the way isset works. Here is some Javascript code that makes a call using PHP: // USER CAME BACK TO CHANGE LANGUAGE if (<?echo isset($_REQUEST)?>) {...
3
by: Olivier Noblanc ATOUSOFT | last post by:
Hello What is the equivalent function of php isset() in python Thank you very much. olivier noblanc http://www.logiciel-erp.fr
1
by: toedipper | last post by:
Hello, PHP4 and MySql I have the code below, a mixture of handcoded and Dreamweaver genaratd php code. Basically it's an update record form - I load the values from a db and bind text boxes...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
3
by: lawrence k | last post by:
One thing I like about Ruby is the use of symbols when passing function parameters to a function. One thing I dislike about PHP is that if a function has 4 optional parameters but you want to do...
3
by: MD | last post by:
Hi, I have a variable which is defined inside a class method. When I call PyModule_GetDict on the module containing this class, the dictionary doesn't contain any information about this...
0
by: Erwin Moller | last post by:
Hi group, I found something strange in PHP5.2.4 (on IIS7/Vista). I am working on an app that has been running just fine under heavy load for over a year at some custumer of mine. (they have...
2
by: John Murtari | last post by:
Folks, Been playing around with developing a 'catch all' convenience function that could be used to properly screen form input via a browser to a script. We are ASSuming register globals is...
1
by: Beamor | last post by:
function art_menu_xml_parcer($content, $showSubMenus) { $doc = new DOMDocument(); $doc->loadXML($content);//this is the line in question $parent = $doc->documentElement; $elements =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.