Making good use of reduce() to traverse dotted attribute of an object.
1 2 3 4 5 6 7 8 9 10 11 12 | class Klass(object):
def __getattr__(self, name):
"""
Locate the function with the dotted
attribute.
"""
def traverse(parent, child):
if instance(parent, str):
parent = getattr(self, parent)
return getattr(parent, child)
return reduce(traverse, name.split('.'))
|