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.
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:
- A proxy object using __getattr__
- Generic desciptor - an ORM column sketch
- the rudimentary @property, method, staticmethod reimplemented in pure Python (explained here and elsewhere), which lead to
- SQLAlchemy's `@hybrid_proprerty <http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/hybrid.html>`__
- Pyramid's deceptively simple memoizing decorator, `@reify <http://docs.pylonsproject.org/projects/pyramid/en/latest/api/decorator.html>`__
- An "Unpacked" tuple properties example to drive home the idea that descriptors can do more than provide attribute access (and mention weak dicts as a way to non-intrusively store data on an object)
(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.