Alex’s answer was good, but providing you with a sample code since you asked for it 🙂
class foo:
def __init__(self):
self.a = "a"
def __getattr__(self, attribute):
return "You asked for %s, but I'm giving you default" % attribute
>>> bar = foo()
>>> bar.a
'a'
>>> bar.b
"You asked for b, but I'm giving you default"
>>> getattr(bar, "a")
'a'
>>> getattr(bar, "b")
"You asked for b, but I'm giving you default"
So in short answer is
You use
__getattr__
to define how to handle attributes that are not found
and
getattr
to get the attributes