Pythonic object-oriented programming
We've seen a few important features of Python's approach to object-orientation. Perhaps the most important is that Python lacks a static binding between variable name and type; any type of object can be assigned to any variable. Names are not resolved statically by a compiler. Python's dynamic name resolution means that we can think of our programs as being entirely generic with respect to class.
When we evaluate obj.attribute
or obj.method()
, there are two steps. First the name, attribute
or method
, must be resolved. Second the referenced attribute or method is evaluated.
For the name resolution step, there are several namespaces that are searched to determine what the name means.
The local namespace of the
obj
instance is searched to resolve the name. The object's namespace is available asobj.__dict__
. Attribute names (and values) are generally found in the object's own namespace. Methods, on the other hand, are not generally part of an object instance...