[Python-Dev] Classes and Metaclasses in Smalltalk

Guido van Rossum guido@digicool.com
Wed, 02 May 2001 17:11:07 -0500


> [From Jim Althoff]
> > In the list below, indentation indicates class hieararchy (superclass --
> > subclass)
> The indentation, unfortunately, seems to be destroyed.
[...]
> A question for Jim (this is more Smalltalk than Python related):
> How does the Behaviour class fit into this picture?

Jim responded with a much clearer diagram, and as a bonus an answer to
your question about Behaviour!

> Hi Guido,
> 
> Sorry about the mangled diagram.  It's kind of tricky doing this with just
> text.  :-)    Anyway, below is a -- hopefully -- improved diagram and
> description.
> 
> At the very bottom is an answer to the question about "Behavior".
> 
> Jim
> 
> ==========================================
> 
> Smalltalk-80 (simplified) class/metaclass structure:
> 
> Terminology:
> o A "class" is an object that can be instantiated.
> o A "metaclass" is a class and is one such that when _it_ is instantiated
> _that_ instance is _itself_ a class (which can be instantiated).
> (A metaclass is a specialization of class).
> 
> Essentially,  there are two parallel hierarchies: 1) the class hierarchy
> and 2) the metaclass hierarchy.  The class hierarchy starts with class
> Object.  The metaclass hierarchy starts right below Class with the
> metaclass ObjectMetaClass.
> 
> <none>
> o Object
>     o Class
>         o MetaClass
>         o ObjectMetaClass
>             o ClassMetaClass
>                 o MetaClassMetaClass
> 
> Object is the top of the class hierarchy (and total hierarchy).  It has no
> superclass.  It is the only class that has no superclass.
> Class is a subclass of Object.
> MetaClass is a subclass of Class.
> 
> ObjectMetaClass is also a subclass of Class.
> ClassMetaClass is a subclass of ObjectMetaClass.
> MetaClassMetaClass is a subclass of ClassMetaClass.
> 
> Adding in application classes Rectangle and SpamRectangle then might look
> like:
> 
> <none>
> o Object
>     o Class
>         o MetaClass
>         o ObjectMetaClass
>             o ClassMetaClass
>                 o MetaClassMetaClass
>             o RectangleMetaClass
>                 o SpamRectangleMetaClass
>     o Rectangle
>         o SpamRectangle
> 
> Rectangle is a subclass of Object.
> SpamRectangle is a subclass of Rectangle.
> 
> RectangleMetaClass is a subclass of ObjectMetaClass.
> SpamRectangleMetaClass is a subclass of RectangleMetaClass.
> 
> Rectangle is an instance of RectangleMetaClass.
> SpamRectangle is an instance of SpamRectangleMetaClass.
> (SpamRectangleMetaClass is an instance of MetaClass.)
> 
> The next list shows both the subclass- and the instanceOf- relationships
> between classes and metaclasses.
> 
> In this list a class listed below another class is a subclass of it.
> SpamMC is an abbreviation for SpamMetaClass (the metaclass of class Spam --
> the class of which class Spam is an instance).
> 
> <none>                Class
> Object    instanceOf  ObjectMC    instanceOf  MetaClass
> Class     instanceOf  ClassMC     instanceOf  MetaClass
> MetaClass instanceOf  MetaClassMC instanceOf  MetaClass
> 
> ObjectMetaClass, ClassMetaClass, and MetaClassMetaClass are all instances
> of MetaClass.
> 
> MetaClass is an instance of MetaClassMetaClass  But MetaClassMetaClass is
> an instance of MetaClass.  So this particular relationship is circular.
> (In Smalltalk-76, Class was an instance of itself.)
> 
> Application classes would have a similar, parallel hierarchy between
> classes and their associated metaclasses.  For example:
> 
> Object        instanceOf ObjectMC        instanceOf MetaClass
> Rectangle     instanceOf RectangleMC     instanceOf MetaClass
> SpamRectangle instanceOf SpamRectangleMC instanceOf MetaClass
> 
> When you create class SpamRectangle as a subclass of class Rectangle, the
> code in the class-creation method first creates the metaclass
> SpamRectangleMetaClass -- by instantiating MetaClass -- as a subclass of
> RectangleMetaClass.  The code then creates the SpamRectangle class as an
> instance of the SpamRectangleMetaClass metaclass it just created.
> 
> You can then create instances of class SpamRectangle.
> 
> SpamRectangle "instance methods" reside in the method dict of
> SpamRectangle.
> SpamRectangle "class methods" reside in the method dict of
> SpamRectangleMetaClass.
> 
> ============================
> 
> Regarding Thomas' question:
> 
> The Smalltalk-80 class hierarchy actually has a bit more factoring than
> what I show above.  In particular, Class and MetaClass are subclasses of
> the class ClassDescription.  ClassDescription is a subclass of class
> Behavior.  Behavior is a subclass of Object.
> 
> So it looks like:
> 
> <none>
> o Object
>     o Behavior
>         o ClassDescription
>             o MetaClass
>             o Class
>                 o ObjectMetaClass
>                     o BehaviorMetaClass
>                         o ClassDescriptionMetaClass
>                             o MetaClassMetaClass
>                             o ClassMetaClass
> 
> Class Behavior basically abstracts the creation and handling of method
> dict.s.  Class ClassDescription factors out common, reusable code between
> MetaClass and Class.  Clearly there are a number of ways of designing (or
> over-designing <wink> ) this part of the hierarchy.  The key idea, though,
> was to use the subclassing mechanism as a way of supportig specialized
> class methods.
> 
> =============================

--Guido van Rossum (home page: http://www.python.org/~guido/)