getattr
is a built-in function taking (at least) two arguments: the object from which you're getting the attribute, and the string name of the attribute.
If the string name is a constant, say 'foo'
, getattr(obj, 'foo')
is exactly the same thing as obj.foo
.
So, the main use case for the built-in function getattr
is when you don't have the attribute name as a constant, but rather as a variable. A second important use case is when you pass it three arguments, rather than just two: in that case, if the attribute is absent from the object, getattr
returns the third, "default", argument, rather than raising an exception.
__getattr__
is a special method, defined in a class, that gets invoked when some attribute of an instance of that class is requested, and other normal ways to supply that attribute (via the instance's __dict__
, slots, properties, and so on) all failed. You can define it, for example, when you want to delegate otherwise-undefined attribute lookups to other objects.
So your second example is wrong because the builtin getattr
can never be called with a single argument.
The third one fails because the dictionary you're trying to "get an attribute" from does not have that attribute -- it has items, which are totally disjoint from attributes of course.