From 7b877a9278b5e8a3dcac5d3a485ed657ac8815fe Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 8 Sep 1997 02:20:57 +0000 Subject: Some nits, added a whole section on what you could do with metaclasses, and acks. --- Demo/metaclasses/index.html | 93 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 90 insertions(+), 3 deletions(-) diff --git a/Demo/metaclasses/index.html b/Demo/metaclasses/index.html index 269dc69..ee91f8f 100644 --- a/Demo/metaclasses/index.html +++ b/Demo/metaclasses/index.html @@ -23,7 +23,7 @@ HREF="http://maigret.cog.brown.edu/pyutil/">MESS package; Jim Fulton has used it in his Extension Classes package. (It has also been referred to as the ``Don -Beaudry hack, but that's a misnomer. There's nothing hackish +Beaudry hack,'' but that's a misnomer. There's nothing hackish about it -- in fact, it is rather elegant and deep, even though there's something dark to it.) @@ -182,6 +182,11 @@ same role at different abstraction levels. And perhaps at some point in the future they will really be the same thing (at which point you would be able to derive subclasses from built-in types). +

At this point it may be worth mentioning that C.__class__ is the +same object as B.__class__, i.e., C's metaclass is the same as B's +metaclass. In other words, subclassing an existing class creates a +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 @@ -229,7 +234,7 @@ assume that no constructor arguments are required:

 x = MySpecialClass()
-y = Myspecialclass()
+y = MySpecialClass()
 print x.__class__, y.__class__
 
@@ -468,7 +473,7 @@ hooks tough; we provide a similar hook _getattr_ instead.

Eiffel.py -ppp +
Uses the above generic metaclass to implement Eiffel style pre-conditions and post-conditions. @@ -481,6 +486,12 @@ methods.

+

Simple.py + +
The example module used above. + +

+

A pattern seems to be emerging: almost all these uses of @@ -493,6 +504,82 @@ Synch module, and it would be interesting to add preconditions to it as well. This needs more research. Perhaps a metaclass could be provided that allows stackable wrappers... +


+ +

Things You Could Do With Metaclasses

+ +

There are lots of things you could do with metaclasses. Most of +these can also be done with creative use of __getattr__, but +metaclasses make it easier to modify the attribute lookup behavior of +classes. Here's a partial list. + +

+ +

+ +

+ +


+ +

Credits

+ +

Many thanks to David Ascher and Donald Beaudry for their comments +on earlier draft of this paper. Also thanks to Matt Conway and Tommy +Burnette for putting a seed for the idea of metaclasses in my +mind, nearly three years ago, even though at the time my response was +``you can do that with __getattr__ hooks...'' :-) + +

+ +


+ -- cgit v0.12