473,386 Members | 1,962 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,386 software developers and data experts.

find isset() php function equivalent in python

Hello
What is the equivalent function of php isset() in python

Thank you very much.

olivier noblanc
http://www.logiciel-erp.fr
Jul 18 '05 #1
3 8585
Olivier Noblanc ATOUSOFT wrote:
Hello
What is the equivalent function of php isset() in python

Thank you very much.

olivier noblanc
http://www.logiciel-erp.fr


try:
if variable:
# isset
pass
except NameError:
# not set
pass

could work...

--
--------------------------------------
Ola Natvig <ol********@infosense.no>
infoSense AS / development
Jul 18 '05 #2
Ola Natvig wrote:
Olivier Noblanc ATOUSOFT wrote:
Hello
What is the equivalent function of php isset() in python


try:
if variable:
# isset
pass
except NameError:
# not set
pass


you could use:
'variable' in vars()
But be aware that it is bad bad practice to do it like that.

If you need variables that you don't know that name of, you should put
them in a dictionary. They are made for that exact purpose.
unkown_vars = {}
unkown_vars['variable'] = 42
'variable' in unkown_vars

True
--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
Jul 18 '05 #3
Max M wrote:
Ola Natvig wrote:
Olivier Noblanc ATOUSOFT wrote:
Hello
What is the equivalent function of php isset() in python

try:
if variable:
# isset
pass
except NameError:
# not set
pass

you could use:
>>> 'variable' in vars()
But be aware that it is bad bad practice to do it like that.

If you need variables that you don't know that name of, you should put
them in a dictionary. They are made for that exact purpose.
>>> unkown_vars = {}
>>> unkown_vars['variable'] = 42
>>> 'variable' in unkown_vars

True


If it's a greater possibility that the 'variable' are set than it's not
you will get better performance when using:

try:
print unknown_vars['variable']
except KeyError:
print 'variable are not set'

istead of

if 'variable' in unknown_vars:
print unknown_vars['variable']
else:
print 'variable are not set'

You could even use

print unknown_vars.get('variable', 'variable are not set')

dictionary.get(key, default) returns the default if key are not located
in the dictionary, I'm not sure if the function uses the try / except
KeyError aproach or what it uses.

--
--------------------------------------
Ola Natvig <ol********@infosense.no>
infoSense AS / development
Jul 18 '05 #4

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

Similar topics

3
by: Greg Yasko | last post by:
Hi. Does anyone know if there's an equivalent of Perl's file::find module in Python? It traverses a directory. I've googled extensively and checked this newsgroup and can't find anything like it...
6
by: Peter Hansen | last post by:
Greetings. Im trying to write a program that can be run from the command line. If I want to search for example after a file with the ending .pdf, I should be able to write in the command line:...
40
by: Xah Lee | last post by:
is it possible in Python to create a function that maintains a variable value? something like this: globe=0; def myFun(): globe=globe+1 return globe
5
by: lugal | last post by:
This might be more appropriate here. I'm new to C++, coming from a background in another languages that allowed a similar solution to work (Python). I wrote the following code in C++ based on the...
2
by: ycoci0 | last post by:
Hi all, Not exactly new to Python, just have not programmed a time dependent function using it before. I imagine that many of you may also program some JavaScript and may be familiar with...
4
by: maborak | last post by:
loading.............. var a =function() { this.b=function(){}; } //howto isset this.b ?
8
by: Giovanni R. | last post by:
Take a look at this code (you can execute it): error_reporting(E_ALL); function byVal( $v) {} function byRef(&$v) {} print '<pre>'; byVal ($first); // gives a notice
4
by: Thomas Mlynarczyk | last post by:
Hello, I have two arrays like this: $aSearch = array( 'A', 'B', 'C.D', 'E', 'F' ); $aSubject = array( 'A' =0, 'A.B' =1, 'X' =2, 'C.D.E' =3, 'A.B.C' => 4 ); Now I want to search $aSubject...
0
by: Michael Fesser | last post by:
..oO(Daniel Molina Wegener) They have side effects or don't work properly. If the variable or element you want to check doesn't exist, then * the first and fourth will create it and set it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
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...

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.