Contribute Media
A thank you to everyone who makes this possible: Read More

The Magic of Attribute Access

Summary

Have you ever wondered how the "self" argument appears when you call a method? Did you know there is a general mechanism behind it? Come learn all about attributes and descriptors.

Description

The first part of this talk will describe what exactly happens when you read or write an attribute in Python.

While this behavior is, of course, explained in the Python docs, more precisely in the Data model section and related writeups, the documentation gives one a "bag of tools" and leaves combining them to the reader.

This talk, on the other hand, will present one chunk of functionality, the attribute lookup, and show how its mechanisms and customization options work together to provide the flexibility (and gotchas) Python provides. The topics covered will be:

  • method resolution order, with a nod to the C3 algorithm
  • instance-, class-, and metaclass-level variables
  • __dict__ and __slots__
  • data/non-data descriptors
  • special methods (__getattr__, __getattribute__, __setattr__, __dir__)

In the second part of the talk, I will show how to use the customization primitives explained before on several interesting and/or useful examples:

(These are subject to change as I compose the talk. Also some examples may end up interleaved with the theory.)

Hopefully I'll have time to conclude with a remark about how Python manages to be a "simple language" despite having these relatively complex mechanisms.

Improve this page