59

I have an object myobject, which might return None. If it returns None, it won't return an attribute id:

a = myobject.id

So when myobject is None, the stament above results in a AttributeError:

AttributeError: 'NoneType' object has no attribute 'id'

If myobject is None, then I want a to be equal to None. How do I avoid this exception in one line statement, such as:

a = default(myobject.id, None)
Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
alwbtc
  • 28,057
  • 62
  • 134
  • 188

7 Answers7

135

You should use the getattr wrapper instead of directly retrieving the value of id.

a = getattr(myobject, 'id', None)

This is like saying "I would like to retrieve the attribute id from the object myobject, but if there is no attribute id inside the object myobject, then return None instead." But it does it efficiently.

Some objects also support the following form of getattr access:

a = myobject.getattr('id', None)

As per OP request, 'deep getattr':

def deepgetattr(obj, attr):
    """Recurses through an attribute chain to get the ultimate value."""
    return reduce(getattr, attr.split('.'), obj)
# usage: 
print deepgetattr(universe, 'galaxy.solarsystem.planet.name')

Simple explanation:

Reduce is like an in-place recursive function. What it does in this case is start with the obj (universe) and then recursively get deeper for each attribute you try to access using getattr, so in your question it would be like this:

a = getattr(getattr(myobject, 'id', None), 'number', None)

Alan
  • 1,889
  • 2
  • 18
  • 30
Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
11

The simplest way is to use the ternary operator:

a = myobject.id if myobject is not None else None

The ternary operator returns the first expression if the middle value is true, otherwise it returns the latter expression.

Note that you could also do this in another way, using exceptions:

try:
    a = myobject.id
except AttributeError:
    a = None

This fits the Pythonic ideal that it's easier to ask for forgiveness than permission - what is best will depend on the situation.

Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
  • Does it evaluate fast? I have 30000 objects to do this check. – alwbtc Feb 17 '13 at 16:50
  • @alwbtc Try it and see. Check out the `timeit` module. If you think it is very likely a large proportion of values will be `None`, the first method will probably be faster, otherwise, use the second. – Gareth Latty Feb 17 '13 at 16:51
  • OK, None values are not much. So I will use the second method. But I also want the code consisting of one-liners. Don't you think one-line statements are better? – alwbtc Feb 17 '13 at 16:54
  • 3
    @alwbtc: No, readable code is better. – Tim Pietzcker Feb 17 '13 at 16:56
  • 1
    @alwbtc Something being on one-line means nothing, in fact, it's generally a *bad* sign as most things are too complex to push onto a single line. Go for readable, then performance, then concise. – Gareth Latty Feb 17 '13 at 17:00
  • 1
    @alwbtc [My answer](http://stackoverflow.com/a/14923509/1561176) provides a one-liner, which is readable, and efficient, and approved by Lattyware... :P take a look. – Inbar Rose Feb 17 '13 at 17:05
6

in my object class you can put override

class Foo(object):
   def __getattribute__(self, name):
      if not name in self;
        return None;
      else:
        # Default behaviour
        return object.__getattribute__(self, name)
Black Diamond
  • 361
  • 5
  • 16
4

If you want to solve the problem in the definition of the class of myobject (like in Black Diamond's answer) you can simply define __getattr__ to return None:

class Myobject:
    def __getattr__(self, name):
        return None

This works because __getattr__ is only called when trying to access an attribute that does not exist, whereas __getattribute__ is always called first no matter the name of the attribute. (See also this SO post.)

To try out:

myobject = Myobject()
print myobject.id 
myobject.id = 7
print myobject.id
matec
  • 1,316
  • 1
  • 10
  • 22
3

Help on built-in function getattr in module builtins:

getattr(...)
    getattr(object, name[, default]) -> value

Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y. When a default argument is given, it is returned when the attribute doesn't exist; without it, an exception is raised in that case.

Following should work:

a = getattr(myobject, 'id', None)
Rob Wouters
  • 15,797
  • 3
  • 42
  • 36
shantanoo
  • 3,617
  • 1
  • 24
  • 37
0
try:
    a = myobject.id
except AttributeError:
    a = None

Will also work and is clearer, IMO

hd1
  • 33,938
  • 5
  • 80
  • 91
  • I"m not python specialist, but in most of the languages I used to deal with, falling in the "except" statement has an additional cost that is non substantial. In a simple case like this one, I would recommend the "getattr" approch (assuming this is less costly). Can some of you tell some more about the getattr method ? – Gauthier Boaglio Feb 17 '13 at 23:26
  • premature optimisation, good lad, premature optimisation – hd1 Feb 17 '13 at 23:29
  • 2
    This isn't premature optimization. Using exceptions to handle normal behaviour is generally considered bad style. – francoisr Dec 09 '15 at 17:22
  • Python's exceptions are not **that** costly and this is the way it is shown throughout the interpreter source. – hd1 Dec 09 '15 at 22:11
0
a=myobect.id if myobject else None
Anil
  • 578
  • 4
  • 12