Instantiating classes which are derived from built-in types.

Alex Martelli aleax at mail.comcast.net
Mon Nov 28 22:08:42 EST 2005


Achim Dahlhoff <Achim.Dahlhoff at t-online.de> wrote:

> Hi.
> 
> I'm trying to find out the diffrence between normal classes and classes
> derived from built-in types.
> (Which is causing me trouble trying to instantiate a class using C API
> calls)
> 
> >>> class A:
> ...  pass
> ...
> >>> class B(dict):
> ...  pass
> ...
> >>> type(A)
> <type 'classobj'>
> >>> type(B)
> <type 'type'>
> >>>

A is oldstyle -- a wart existing for backwards compatibility.  Newstyle
classes (highly recommended for all new code) may inherit from object
(or any other newstyle class, or builtin type supporting inheritance) or
may be set on a per-module basis by setting __metaclass__=type at module
level at the start of the module.

> Anyone know how an object could be instantiated using a handle to B?

Just use the recommended abstract interface, e.g.
PyObject_CallFunction(B, NULL) if you want to pass no arguments - this
works for A, for B, and for any other callable including e.g. a factory
function (its very generality makes it very desirable...).


Alex



More information about the Python-list mailing list