Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

checking if object exists in python

Posted on 2010-09-08
4
Medium Priority
?
1,162 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 29

Accepted Solution

by:
pepr earned 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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

927 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