from functools import reduce
def deepgetattr(obj, attr):
    """Recurses through an attribute chain to get the ultimate value."""
return reduce(getattr, attr.split('.'), obj)
print(deepgetattr(universe, 'galaxy.solarsystem.planet.name'))
How can I return a default value for an attribute?