class "type" and metaclasses (was Re: Style for overwritten methods of abstract classes)

Martin von Loewis loewis at informatik.hu-berlin.de
Sun Jan 6 11:58:03 EST 2002


Stefan Schwarzer <s.schwarzer at ndh.net> writes:

> To be honest, I haven't understood everything of this but it inspired
> my curiosity regarding the "type" class. I didn't find anything about
> it in the Python 2.2 docs (though I won't say it's not there ;-) ).

The type class is just what used to be the type type in earlier Python
versions, i.e. <type 'type'>, see

http://www.python.org/doc/current/lib/bltin-type-objects.html

It is just that the builtin type() function is not a function anymore,
but the type object itself.

> The PEPs 252 and 253 seemed rather to be aimed at C programmers who
> want to define their own types. The most useful document (for me) seems
> to be the tutorial "Unifying types and classes in Python 2.2" at
> http://www.python.org/2.2/descrintro.html . If I haven't overlooked it
> that also doesn't mention the type class, does it?

The type class is nothing new in Python, it was always there.

> - Where can I find more information on metaclass programming in Python?

At the moment: Use the source, Luke. Volunteers to write documentation
are highly encouraged to do so.

> - (How) are the built-in function type and the class named "type" (that
>   didn't exist prior to Python 2.2?) related?

The builtin is not a function anymore, it is a type. It existed
before, as types.TypeType. Since types are callable now, many builtins
have changed from functions to types (like int, float, str).

> - Should I be very concerned with metaclass programming? 

No.

>   On the other hand, I suppose that would not have been added to
>   Python if it weren't useful (contrasted to beeing merely
>   "nice"). Are metaclasses rather for experts?

The prime rationale for adding them was the ability to have types
implemented in C to be used as base classes for Python-defined
classes; this was not possible before. The prime rationale for that,
in turn, was the desire to get rid of the ExtensionClasses
infrastructure, in particular inside Zope. 

So unless you have written ExtensionClasses before, you don't need to
worry about meta-programming. If you have written extension classes,
you should consider moving to 2.2 style types/classes ASAP, to let us
better understand how usable the existing API is.

Please expect that many things will change in the future in this area;
there is a desire to add "proper syntax" for many of the features
which are currently done through conventions only (e.g. __slots__,
__metaclass__).

Regards,
Martin



More information about the Python-list mailing list