Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

checking if object exists in python

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 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 while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

730 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