From 7ade6da8660a640651a7e37f1b77b2721045942b Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Thu, 11 Sep 1997 22:54:49 +0000 Subject: [PATCH] As Paul Prescod pointed out, metaprogramming is really something different (programs that write programs). We are dealing with metaclasses here. So change the words slightly. --- Demo/metaclasses/index.html | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/Demo/metaclasses/index.html b/Demo/metaclasses/index.html index ee91f8f1a2..52d52e9fd0 100644 --- a/Demo/metaclasses/index.html +++ b/Demo/metaclasses/index.html @@ -1,18 +1,16 @@ -Metaprogramming in Python 1.5 +Metaclasses in Python 1.5 -

Metaprogramming in Python 1.5 (DRAFT)

+

Metaclasses in Python 1.5

+

(A.k.a. The Killer Joke :-)

-

XXX This is very much a work in progress.

- -

While Python 1.5 is only out as a restricted alpha -release, its metaprogramming feature is worth mentioning. +

Note: this document describes a feature only released in Python 1.5a3.

In previous Python releases (and still in 1.5), there is something called the ``Don Beaudry hook'', after its inventor and champion. @@ -144,7 +142,7 @@ class C: the class namespace's contents would be {'a': 1, 'f': <function f ...>}. -

But enough already about Python metaprogramming in C; read the +

But enough already about writing Python metaclasses in C; read the documentation of MESS or Extension @@ -153,7 +151,7 @@ Classes for more information.

Writing Metaclasses in Python

In Python 1.5, the requirement to write a C extension in order to -engage in metaprogramming has been dropped (though you can still do +write metaclasses has been dropped (though you can still do it, of course). In addition to the check ``is the type of the base class callable,'' there's a check ``does the base class have a __class__ attribute.'' If so, it is assumed that the __class__ @@ -189,8 +187,8 @@ new (meta)inststance of the base class's metaclass.

Going back to the example, the class B.__class__ is instantiated, passing its constructor the same three arguments that are passed to -the default class constructor or to an extension's metaprogramming -code: name, bases, and namespace. +the default class constructor or to an extension's metaclass: +name, bases, and namespace.

It is easy to be confused by what exactly happens when using a metaclass, because we lose the absolute distinction between classes @@ -244,7 +242,7 @@ How did this happen? MySpecialClass is an instance of MetaClass1 __call__ method is invoked, and presumably the __call__ method defined by MetaClass1 returns an instance of HelperClass1. -

Now let's see how we could use metaprogramming -- what can we do +

Now let's see how we could use metaclasses -- what can we do with metaclasses that we can't easily do without them? Here's one idea: a metaclass could automatically insert trace calls for all method calls. Let's first develop a simplified example, without @@ -424,7 +422,7 @@ doesn't support all these features yet.

Real-life Examples

Have a look at some very preliminary examples that I coded up to -teach myself how to use metaprogramming: +teach myself how to write metaclasses:

-- 2.40.0