0

Consider a Python object o of class C. What exactly happens when accessing o.foo?

Presumably the runtime first looks to see whether o itself contains foo (in its __dict__ and/or __slots__), then checks C, then the base classes of C.

Is that fundamentally how attribute lookup is performed? If so, presumably the process is more complicated in practice - are other locations checked for foo?

Are there facilities in Python that can be used to customize the above lookup process?

user200783
  • 12,313
  • 10
  • 58
  • 110
  • It's a bit more complicated than that; there's a fairly complete description here: http://stupidpythonideas.blogspot.com/2015/12/how-lookup-works.html – jakevdp Oct 03 '17 at 04:40
  • What is the problem you are trying to solve by customizing the lookup? – Burhan Khalid Oct 03 '17 at 04:42
  • 1
    Possible duplicate of [How python attribute lookup process works?](https://stackoverflow.com/questions/11211233/how-python-attribute-lookup-process-works) – metatoaster Oct 03 '17 at 04:42
  • 1
    [This is worth checking out too](https://blog.ionelmc.ro/2015/02/09/understanding-python-metaclasses/#object-attribute-lookup). You've got to know about the [descriptor protocol, though](https://docs.python.org/3/howto/descriptor.html). – juanpa.arrivillaga Oct 03 '17 at 04:48

0 Answers0