Data model and attribute resolution in subclasses

Adam Preble adam.preble at gmail.com
Fri Feb 28 02:21:12 EST 2020


I have been making some progress on my custom interpreter project but I found I have totally blown implementing proper subclassing in the data model. What I have right now is PyClass defining what a PyObject is. When I make a PyObject from a PyClass, the PyObject sets up a __dict__ that is used for attribute lookup. When I realized I needed to worry about looking up parent namespace stuff, this fell apart because my PyClass had no real notion of a namespace.

I'm looking at the Python data model for inspiration. While I don't have to implement the full specifications, it helps me where I don't have an alternative. However, the data model is definitely a programmer document; it's one of those things where the prose is being very precise in what it's saying and that can foil a casual reading.

Here's what I think is supposed to exist:
1. PyObject is the base.
2. It has an "internal dictionary." This isn't exposed as __dict__
3. PyClass subclasses PyObject.
4. PyClass has a __dict__

Is there a term for PyObject's internal dictionary. It wasn't called __dict__ and I think that's for good reasons. I guess the idea is a PyObject doesn't have a namespace, but a PyClass does (?).

Now to look something up. I assume that __getattribute__ is supposed to do something like:
1. The PyClass __dict__ for the given PyObject is consulted.
2. The implementation for __getattribute__ for the PyObject will default to looking into the "internal dictionary."
3. Assuming the attribute is not found, the subclasses are then consulted using the subclass' __getattribute__ calls. We might recurse on this. There's probably some trivia here regarding multiple inheritance; I'm not entirely concerned (yet).
4. Assuming it's never found, then the user sees an AttributeError

Would each of these failed lookups result in an AttributeError? I don't know how much it matters to me right now that I implement exactly to that, but I was curious if that's really how that goes under the hood.


More information about the Python-list mailing list