Solved

checking if object exists in python

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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 …

630 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