liveBook · Manning

Class_

As explained in The Quick Python Book, Fourth Edition

Class_ is a book-specific example class containing methods and class variables for demonstrating namespace lookup.

`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

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Works through printed namespaces, lookup order, class-variable assignment, and private-name access; does not detail Python’s lookup implementation.
Examples
Uses `cs.Class_()` and `method_1()` output showing locals, globals, instance attributes, class members, and inherited members.
What it assumes
Assumes general programming experience but no prior Python knowledge or familiarity with a particular object-oriented language.
What you can do after
Diagnose/debug with it by checking which namespace supplies a name and whether access requires `self` or the class name.
How it connects
Connects namespace lookup with inheritance, class variables, instance variables, methods, and name mangling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.