| 1 | <HTML>
|
|---|
| 2 |
|
|---|
| 3 | <HEAD>
|
|---|
| 4 | <TITLE>Metaclasses in Python 1.5</TITLE>
|
|---|
| 5 | </HEAD>
|
|---|
| 6 |
|
|---|
| 7 | <BODY BGCOLOR="FFFFFF">
|
|---|
| 8 |
|
|---|
| 9 | <H1>Metaclasses in Python 1.5</H1>
|
|---|
| 10 | <H2>(A.k.a. The Killer Joke :-)</H2>
|
|---|
| 11 |
|
|---|
| 12 | <HR>
|
|---|
| 13 |
|
|---|
| 14 | (<i>Postscript:</i> reading this essay is probably not the best way to
|
|---|
| 15 | understand the metaclass hook described here. See a <A
|
|---|
| 16 | HREF="meta-vladimir.txt">message posted by Vladimir Marangozov</A>
|
|---|
| 17 | which may give a gentler introduction to the matter. You may also
|
|---|
| 18 | want to search Deja News for messages with "metaclass" in the subject
|
|---|
| 19 | posted to comp.lang.python in July and August 1998.)
|
|---|
| 20 |
|
|---|
| 21 | <HR>
|
|---|
| 22 |
|
|---|
| 23 | <P>In previous Python releases (and still in 1.5), there is something
|
|---|
| 24 | called the ``Don Beaudry hook'', after its inventor and champion.
|
|---|
| 25 | This allows C extensions to provide alternate class behavior, thereby
|
|---|
| 26 | allowing the Python class syntax to be used to define other class-like
|
|---|
| 27 | entities. Don Beaudry has used this in his infamous <A
|
|---|
| 28 | HREF="http://maigret.cog.brown.edu/pyutil/">MESS</A> package; Jim
|
|---|
| 29 | Fulton has used it in his <A
|
|---|
| 30 | HREF="http://www.digicool.com/releases/ExtensionClass/">Extension
|
|---|
| 31 | Classes</A> package. (It has also been referred to as the ``Don
|
|---|
| 32 | Beaudry <i>hack</i>,'' but that's a misnomer. There's nothing hackish
|
|---|
| 33 | about it -- in fact, it is rather elegant and deep, even though
|
|---|
| 34 | there's something dark to it.)
|
|---|
| 35 |
|
|---|
| 36 | <P>(On first reading, you may want to skip directly to the examples in
|
|---|
| 37 | the section "Writing Metaclasses in Python" below, unless you want
|
|---|
| 38 | your head to explode.)
|
|---|
| 39 |
|
|---|
| 40 | <P>
|
|---|
| 41 |
|
|---|
| 42 | <HR>
|
|---|
| 43 |
|
|---|
| 44 | <P>Documentation of the Don Beaudry hook has purposefully been kept
|
|---|
| 45 | minimal, since it is a feature of incredible power, and is easily
|
|---|
| 46 | abused. Basically, it checks whether the <b>type of the base
|
|---|
| 47 | class</b> is callable, and if so, it is called to create the new
|
|---|
| 48 | class.
|
|---|
| 49 |
|
|---|
| 50 | <P>Note the two indirection levels. Take a simple example:
|
|---|
| 51 |
|
|---|
| 52 | <PRE>
|
|---|
| 53 | class B:
|
|---|
| 54 | pass
|
|---|
| 55 |
|
|---|
| 56 | class C(B):
|
|---|
| 57 | pass
|
|---|
| 58 | </PRE>
|
|---|
| 59 |
|
|---|
| 60 | Take a look at the second class definition, and try to fathom ``the
|
|---|
| 61 | type of the base class is callable.''
|
|---|
| 62 |
|
|---|
| 63 | <P>(Types are not classes, by the way. See questions 4.2, 4.19 and in
|
|---|
| 64 | particular 6.22 in the <A
|
|---|
| 65 | HREF="http://www.python.org/cgi-bin/faqw.py" >Python FAQ</A>
|
|---|
| 66 | for more on this topic.)
|
|---|
| 67 |
|
|---|
| 68 | <P>
|
|---|
|
|---|