Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web.
|
2011-09-23 Python's attribute lookup orderOne of the things that surprised me recently is that the Python
language reference doesn't seem to
document the specifics of how Python searches for the attribute when you
try to access (Such a description really belongs in the language reference because it's an important part of Python's semantics. It's possible that you can reassemble a full description of the lookup semantics from the bits and pieces scattered throughout the language reference.) First, any time Python looks for an attribute on an object it
doesn't just search the object's (nominal) attribute dictionary; it
also searches back through the object's base classes (in method
resolution order), ultimately terminating at
Let's say we're looking for
(As you might guess, the code does not search for Note that Python never looks at the type of (This explains how metaclasses can be used to add class-only
attributes on classes. It's important here to not confuse an object's base classes with its
type or its type's base classes. I tend to think of base classes and
types as creating a two dimensional structure, where inheritance goes
sideways through a class's base classes to end up at (The confusing case is a metaclass, which has Sidebar: where this is done in CPython's codeIn CPython, this is done in two separate pieces of code. Lookups
for classes are handled by (4 comments.)
python/AttributeLookupOrder written at 00:30:31; Add Comment
|
These are my WanderingThoughts GettingAround This is part of CSpace, and is written by ChrisSiebenmann. * * * Atom feeds are available; see the bottom of most pages. Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web |