How can I return a default value for an attribute?

PythonReflectionAttributesDefaultVoid

Python Problem Overview


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)

Python Solutions


Solution 1 - Python

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)

Solution 2 - Python

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.

Solution 3 - Python

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)

Solution 4 - Python

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

Solution 5 - Python

> 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)

Solution 6 - Python

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

Will also work and is clearer, IMO

Solution 7 - Python

a=myobect.id if myobject else None

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionalwbtcView Question on Stackoverflow
Solution 1 - PythonInbar RoseView Answer on Stackoverflow
Solution 2 - PythonGareth LattyView Answer on Stackoverflow
Solution 3 - PythonBlack DiamondView Answer on Stackoverflow
Solution 4 - PythonmatecView Answer on Stackoverflow
Solution 5 - PythonshantanooView Answer on Stackoverflow
Solution 6 - Pythonhd1View Answer on Stackoverflow
Solution 7 - PythonAnilView Answer on Stackoverflow