Solved

checking if object exists in python

Posted on 2010-09-08
4
1,000 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
  • 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 28

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 28

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now