`Class_` is an example Python class for tracing where a method finds names. It inherits from `SuperClass` and defines instance variables, class variables, methods, and private names. Its methods expose the distinction between names available directly and attributes reached through `self`. section 15.11
Inside `Class_.method_1`, the examples separate four places to look:
- The method’s local namespace contains `self`, `param`, and `local_var`. These names can be used directly. section 15.11
- The module’s global namespace contains `mod_var`, `mod_func`, and the class names. These are also directly accessible from the method. section 15.11
- The instance holds `instance_var` and an instance variable initialized by `SuperClass`. Attributes reached through `self` are searched in order: instance, class, then superclass. section 15.11
- The class holds `class_var` and its methods; the superclass supplies inherited class variables and methods. `self` can read `Class_.class_var`, but assignment to that class variable must use `Class_.class_var`. section 15.11
Private names make the lookup boundaries visible. `Class_` can access its own private instance variable, class variable, and method through `self`. The displayed instance and class namespaces show their mangled names. Private names defined by `SuperClass` cannot be accessed from `Class_` through their ordinary `self` spellings. section 15.11
Access instance, class, and superclass namespaces through 'self'
Instance namespace: {'super_instance_var': 'superclass instance variable:
self.super_instance_var ', '_SuperClass__psiv': 'private superclass
instance variable: no access', '_Class___priv_instance_var': 'private
instance variable: self.__priv_instance_var', 'instance_var': 'instance
variable: self.instance_var'}
instance variable: self.instance_var
private instance variable: self.__priv_instance_var
superclass instance variable: self.super_instance_var
Compare the instance’s ordinary attributes with the mangled private names shown in its namespace. section 15.11
Class_ namespace: {'__module__': '__main__', 'class_var': 'class variable:
self.class_var or Class_.class_var (for assignment)',
'_Class___priv_class_var': 'class private variable: self.__priv_class_var
or Class_.__priv_class_var ', '__init__': <function Class_.__init__ at
0x7b4b3e3dbeb0>, 'method_2': <function Class_.method_2 at 0x7b4b3e3db640>,
'_Class___priv_method': <function Class_.__priv_method at 0x7b4b3e3da560>,
'method_1': <function Class_.method_1 at 0x7b4b3e3db520>, '__doc__': None}
class variable: self.class_var or Class_.class_var (for assignment)
method: self.method_2()
class private variable: self.__priv_class_var or Class_.__priv_class_var
private method: self.__priv_method()
Inspect `Class_`’s class variables and methods, including the private names stored under mangled keys. section 15.11
The practical distinction is between a bare name such as a method local or module global and an attribute requested through `self`. When an attribute is not on the instance, lookup can continue to `Class_` and then `SuperClass`; that does not make a private superclass name directly available to the subclass. section 15.11
