Solved

checking if object exists in python

Posted on 2010-09-08
4
1,056 Views
Last Modified: 2012-05-10
I'm calling a method for which i don't know if it returns something (eg:ObjectX) or nothing (eg:null) so I'd like to check if object exists like in java:
ObjectX o = tryGetObjectX()
if (o==null) {
   ...
}
How do I do this in Python?
0
Comment
Question by:infokrt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 5

Expert Comment

by:-Richard-
ID: 33625801
You almost have it.  In Python the keyword which signifiies nonexistence is "None".  In Python, all methods return a value, and if the method is not written to explicitly return a value,then its return value is "None".  Thus your code snippet would look like this:

    o = tryGetObject()
    if o == None:
        ...

Note that in Python the type of a variable is determined by what is assigned to it, not by any explicit declaration, s it is not necessary to declare "o" as an "Object" or anything else.
0
 
LVL 12

Expert Comment

by:w00te
ID: 33626125
In addition to Richard's answer, you may want to read this.  It's just some good examples about using none, empty, and nothing and how they compare to coming from a C background (fairly similar views to Java).  Anyway, it's an easy read and may give you some new perspective on the differences between python & its scripting style vs compiled languages :)
http://boodebr.org/main/python/tourist/none-empty-nothing
-w00te
0
 
LVL 29

Accepted Solution

by:
pepr earned 125 total points
ID: 33626719
It is better and also more readable to use the operator 'is' or 'is not' instead of '==' or '!='.  It compares for identity of objects instead for their value equivalence.  The None is actually singleton object in Python.  The None is the named reference to that object.  You should prefer to return None in your tryGetObject() even though you could return anything that you can detect.

You can also think abou the built-in function isinstance() (see http://docs.python.org/library/functions.html#isinstance) and modify the behaviour according to the class of the returned object.
0
 
LVL 29

Expert Comment

by:pepr
ID: 33626884
In adition to what Richard wrote.  In Python, any variable is actually named reference.  The name is never bound to the object, always to the reference to the object.  The reference is always untyped.  The type is always bound to the target object.  Because of this it seems that variable can change its type.  Actually, variable always contains untyped reference and does not care what type of object is referenced.

Warning: When assigning a variable you always copy only the reference.   The target object is shared (references are counted).  When you need to copy the object, you have do do it somehow more explicitly.  
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question