[Python-Dev] Classes and Metaclasses in Smalltalk (original) (raw)
Guido van Rossum guido@digicool.com
Mon, 21 May 2001 23:22:40 -0400
- Previous message: [Python-Dev] test_mailbox now fails on Windows
- Next message: [Python-Dev] type/class unification and ExtensionClass
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hi Alan,
Thanks a lot for your input. I am cc'ing this reply to python-dev because I think my reply will be interesting for others. (Python-dev'ers: Alan expressed concern that introducing Smalltalk metaclasses would make Python unnecessarily complicated.)
The way my thinking is currently going, it's not likely that Python will get a metaclass system similar to Smalltalk. However, unifying types and classes is useful for other reasons: please go to http://python.sourceforge.net/peps/ to read PEP 252 which explains how introspection can become simpler and more powerful by unifying the introspection mechanisms for types and classes.
There will still be metaclasses, but the metaclasses will be less important than in Smalltalk. Class methods as commonly seen in Smalltalk are not high on my priority list, and the metaclass hierarchy won't be parallelling the regular class hierarchy. Instead, most metaclass programming will be done in C by programmers who want to implement alternative class policies.
For example, the current class implementation gives each class a dict for methods and class variables, and dynamically searches the class hierarchy for methods. An alternative inheritance policy could merge the dict of the base class(es) with the dict of the derived class at class declaration time: this would make method lookup a single dict lookup no matter how many levels of base classes are involved, at the cost of making classes less dynamic, because a change to a base class won't be seen in a derived class. A metaclass controls method lookup and class construction, and thus a different metaclass can be used to change this policy for selected class hierarchies without changing the default policy (which would be backwards incompatible).
Other policies under control of a metaclass could include overriding hooks for getattr and setattr, alternative mechanisms to store instance variables (e.g. slot-based rather than dict-based), and so on.
While I think I can make it possible to write metaclasses in pure Python (by subclassing types.TypeType), I expect that most metaprogramming will be done in C, for performance reasons and for maximum flexibility.
--Guido van Rossum (home page: http://www.python.org/~guido/)
- Previous message: [Python-Dev] test_mailbox now fails on Windows
- Next message: [Python-Dev] type/class unification and ExtensionClass
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]