Alex Martelli, Uber Technical Lead, Google, Inc.
Date: Wednesday, 19 October 2005
Time: 16:00 - 16:45
Location: St. John's Room II
Python's versions since 2.2 have introduced, enhanced, and clarified some new advanced mechanisms: two of them are the underpinnings of Python's Object-Oriented Programming (OOP), Descriptors and Metaclasses; one, introduced in 2.4, is a syntax for the systematic application of an important use case for higher-order functions. This presentation takes its contents mostly from the (informally known as...) "Black Magic" chapter of the Python Cookbook (2nd Edition), also known (official chapter title!) as "Descriptors, Decorators, and Metaclasses," and focuses on some of the most interesting applications and related idioms presented there, with expanded explanations of the underlying language mechanisms and additional examples.
Descriptors control and guide attribute access. Python offers several built-in descriptor types--for example, in today's Python, functions are descriptors--and also lets you code your own.
Decorators are a simple syntax to feed a function as the argument to another (higher-order) function, and have the latter's resulting value used in lieu of the original function--while decorator syntax ('@deco') is new in Python 2.4, you can use the same approach with slightly clumsier syntax ('func = deco(func)') in 2.3 and even 2.2.
Metaclasses are to classes as classes are to instances. Python's built-in metaclass is the built-in 'type,' and you can subclass and customize it to produce custom metaclasses to control behavior of whole family of classes.
This presentation shows how to best use each of these mechanisms, both in terms of existing, Python-supplied objects and types (descriptors, decorators, and metaclasses), and of coding and using your own custom ones. What are "data" and "non-data" descriptors, and how and why might you code and use either kind? When do you need introspection to ensure proper decoration, and how can Python's standard library help there? How should custom metaclasses cooperate to insure peaceful coexistence of several of them, and how can you solve metatype-conflicts? And, of course--how do custom descriptors, decorators, and metaclasses cooperate with each other, and with other powerful Python mechanisms such as introspection, closures, and advanced dynamic gyrations to let you write elegant and powerfully customized code with minimal effort? This presentation explores these kinds of questions, providing the fundamental knowledge and the outlook that will help you answer the questions in the most useful ways for your own applications.