472,331 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Establishing if an Object is Defined

Hi,

The following code works -

one = 1
if one == 1:
ok = 1
print ok

but this does not, without exception -

one = 2
if one == 1:
ok = 1
print ok

How do I establish before printing ok if it actually exists so as to
avoid this exception?

Thanks for your help,

Barry.

Jan 10 '07 #1
2 871
bg***@yahoo.com a écrit :
Hi,

The following code works -

one = 1
if one == 1:
ok = 1
print ok

but this does not, without exception -

one = 2
if one == 1:
ok = 1
print ok

How do I establish before printing ok if it actually exists so as to
avoid this exception?
ok = 0
....do the job...

Very simple, failure-proof, no special case.
Jan 10 '07 #2
bg***@yahoo.com a écrit :
Hi,

The following code works -

one = 1
if one == 1:
ok = 1
print ok

but this does not, without exception -

one = 2
Are you competing for the Most Misleading Name Award(tm) ?-)
if one == 1:
ok = 1
print ok

How do I establish before printing ok if it actually exists so as to
avoid this exception?
The simplest way is to make sure the name will be defined whatever the
value of the test:

one = 42
# ...
ok = (one == 1)
print ok
As a side note, if you want to check wether a name exists in the current
namespace, you can use a try/except block:

try:
toto
print "toto is defined"
except NameError:
print "toto is not defined"
Jan 10 '07 #3

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

Similar topics

8
by: gs | last post by:
Hi! This is my first time posting to a newsgroup so please be gentle to me :) Introduction to my problem: I'm studying at the university...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There...
3
by: thom | last post by:
Using Vs2005 sqlServer 2005 When i try to connect i get this error: An error has occurred while establishing a connection to the server. When...
0
by: Tor Inge Rislaa | last post by:
Just for testing deployment of an asp.net application to the remote server I have crated an application based on the StarterKit application in...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts,...
21
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } ...
0
by: gnewsgroup | last post by:
Sorry for the cross-post. I do have this posted in the aspnet security group. I notice there are a lot of "persecution" spams over there and I am...
0
by: aboutjav.com | last post by:
Hi, I need some help. I am getting this error after I complete the asp.net register control and click on the continue button. It crashed when it...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

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.