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