From parsing a class to code object to class to mappingproxy to object (oh my!)
greg.ewing at canterbury.ac.nz
Mon Apr 1 02:23:25 EDT 2019
adam.preble at gmail.com wrote:
> What is the plumbing taking the result of that code object over to this
> proxy? I'm assuming __build_class__ runs that code object and then starts
> looking for new names and see to create this.
> Is this mapping proxy the
> important thing for carrying the method declaration?
No, the mapping proxy is only there to prevent Python code from directly
accessing the dict holding the class's attributes. (If that were
allowed, bad things could be done that would crash the interpreter.)
There's a fairly good analysis of what __build_class__ does here:
Briefly, it creates a dict to serve as the class's namespace dict,
then executes the class body function passed to it, with that dict
as the local namespace. So method defs and other assignments go
straight into what will become the class namespace when the class
object is created.
> I'm then assuming that in object construction, this proxy is carried by some
> reference to the final object in such a way that if the class's fields are
> modified, all instances would see the modification unless the local object
> itself was overridden.
There's nothing fancy going on at that stage. The object contains
a reference to its class, that's all. If an attribute is not found
in the object, it is then looked for in the namespace of its class,
then its base classes in mro order. (Actually it's more complicated
than that to allow for descriptors, but that's the basic idea.)
More information about the Python-list