By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,122 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

Instantiating classes which are derived from built-in types.

P: n/a
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'>

When I have a handle to A as a PyObject, I can create an instance using
PyInstance_New(). When I have a handle to B, this does not work as the
function wants a class PyObject.
I found that the API function PyType_GenericNew() can create something from
the B type, but it does not call any constructors.

(using the 'dict' here is an example. In my case, I'm using a self-defined
type I'm using as an API into the C++ part of the software.)

Anyone know how an object could be instantiated using a handle to B?
thanks,
Achim Dahlhoff.
Nov 28 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Achim Dahlhoff <Ac************@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
Nov 29 '05 #2

P: n/a
al***@mail.comcast.net (Alex Martelli) writes:
A is oldstyle -- a wart existing for backwards compatibility.


I think it's time for "from __future__ import newclasses" since
I hate having to type "class A(object):" instead of "class A:"
all over the place.
Nov 29 '05 #3

P: n/a
Paul Rubin <http://ph****@NOSPAM.invalid> wrote:
al***@mail.comcast.net (Alex Martelli) writes:
A is oldstyle -- a wart existing for backwards compatibility.


I think it's time for "from __future__ import newclasses" since
I hate having to type "class A(object):" instead of "class A:"
all over the place.


Use __metaclass__=type exactly in the same location where you'd want to
import from __future__ and you will get exactly the same functionality.
Alex
Nov 29 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.