Attribute Lookup Chain — ELI5

Imagine you’re looking for your keys. You check your pockets first. Not there? You check your bag. Still not there? You check the kitchen counter. Then the hook by the door. You always search in the same order, and you stop as soon as you find them.

Python does the exact same thing when you write dog.name. It doesn’t magically know where name lives. It searches through a specific list of places, in order, and uses the first match it finds.

The Search Order

  1. The object itself — Did someone put name directly on this specific dog?
  2. The class — Did the Dog class define name for all dogs?
  3. Parent classes — Did a parent class (like Animal) define name?

If Python checks everywhere and finds nothing, it throws an error — like you giving up and saying “I can’t find my keys anywhere!”

Why Does This Matter?

Because the order determines what you get. If both the dog object AND the Dog class have something called name, you get the one from the object — because Python checks there first.

It’s like asking “What’s the rule?” You follow your own version first. Only if you don’t have one do you check what your family says. And only if your family doesn’t have one do you look at what society says.

A Surprise Twist

There’s actually a secret step that can jump the line: something called a descriptor. Think of it as a VIP that gets checked even before the object’s own stuff. Properties (the @property thing) are descriptors, and they can override what an object thinks it has.

One thing to remember: When you access obj.attr, Python searches in a specific order — and the first place it finds the attribute wins.

pythonadvancedoopinternals

See Also