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 6 47339
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******** *************** *************** @python.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
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(myV ar)
except NameError:
print "myVar is not set"
/Esben
"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
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>
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.
In article <ma************ *************** ***********@pyt hon.org>,
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" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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)?>) {
alert("WORKS1");
desiredLanguage = "<?echo $_REQUEST?>";
|
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
|
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 etc. This works ok. But I also have an update
action - if a user changes any of the text box values then he can click
update and the code should write the new values to the db. Should, but
it does'nt! When I go and look in the db I can see...
|
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 problem is
that my little helper function doesn't work! It claims that a variable
doesn't exist. If I move the variable declaration, it finds the
variable, but can't change it. Declaring the variable global in the
nested function doesn't work...
|
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
something with the 4th one, you need to put in something, perhaps
empty strings, for the first 3 parameters, even though you've no
interest in using them.
I'm wondering if there is anyway to emulate the Ruby style with
something like this:
| |
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 variable. Is
this expected behavior? If so, what options do I have to access this
variable from my Python C extension.
Thanks and Regards,
-MD
|
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 PHP5.1 on W2003
Server, ISAPI)
I imported the whole project locally on a slightly more modern version
of PHP 5.2.4 to make a few adjustments.
|
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 OFF and magic quote are OFF. The
function below would be used like:
$user_email = script_param("user_email_addr");
|
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 = $parent->childNodes;
need help. my site worked fine on my localhost but when i uploaded it to my live server i keep getting this error
need help to recode line to match my hosting server.
im using php 5
i have attached the common_method file
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |