Jim Fulton, Digital Creations, Inc. jim@digicool.com
Copyright (C) 1996-1998, Digital Creations.
A lightweight mechanism has been developed for making Python extension types more class-like. Classes can be developed in an extension language, such as C or C++, and these classes can be treated like other python classes:
They can be sub-classed in python,
They provide access to method documentation strings, and
They can be used to directly create new instances.
An example class shows how extension classes are implemented and how they differ from extension types.
Extension classes provide additional extensions to class and instance semantics, including:
A protocol for accessing subobjects "in the context of" their containers. This is used to implement custom method types and environmental acquisition.
A protocol for overriding method call semantics. This is used to implement "synchonized" classes and could be used to implement argument type checking.
A protocol for class initialization that supports execution of a
special __class_init__
method after a class has been
initialized.
Extension classes illustrate how the Python class mechanism can be extended and may provide a basis for improved or specialized class models.
To find out what's changed in this release, see the release notes.
Currently, Python provides two ways of defining new kinds of objects:
Python classes
Extension types
Each approach has it's strengths. Extension types provide much greater control to the programmer and, generally, better performance. Because extension types are written in C, the programmer has greater access to external resources. (Note that Python's use of the term type has little to do with the notion of type as a formal specification.)
Classes provide a higher level of abstraction and are generally much easier to develop. Classes provide full inheritance support, while support for inheritance when developing extension types is very limited. Classes provide run-time meta-data, such as method documentation strings, that are useful for documentation and discovery. Classes act as factories for creating instances, while separate functions must be provided to create instances of types.
It would be useful to combine the features of the two approaches. It would be useful to be able to have better support for inheritance for types, or to be able to subclass from types in Python. It would be useful to be able to