473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Instantiating classes which are derived from built-in types.

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
3 1456
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Suzanne Vogel | last post by:
** Is there a way (C++ syntax) to specify that a pointer should refer to an object that derives from *two* specific classes? eg, Suppose I have two classes, 'A' and 'B', and I want to specify...
3
by: Mikael Olofsson | last post by:
Hi! I've asked Google, but have not found any useful information there. Situation: I have a base class, say >>> class base(object): ImportantClassAttribute = None Now, I want to...
3
by: SammySAm | last post by:
I have a set of XSD's that I have to instantiate at run time and pass some values in C++.Net. How can I acheive it? Please advice. Best, Sam
2
by: Peter Bates | last post by:
Hi, I'm just getting used to XSDObjectGen and i have the following question. Can i use a class inherited from a class generated by XSDObjectGen with XmlSerialize? Specifically, I have many...
2
by: Joe Vrba | last post by:
I'm building a family of components derived from UserControl. There's an abstract base class to ensure basic functionality and then numerous other controls derived from that. The problem is...
2
by: active | last post by:
Because of an example I followed I've been instantiating Image objects. Now I notice that the documentation says the Image class is an abstract base class. Things seem to be working! Is the...
3
by: zlf | last post by:
I am asked to complete a COM+ component, there is a class A derived from ServicedComponent. However, when executing , exception is thrown. Messaged: Unhandled Exception:...
15
by: Jeff Mason | last post by:
Hi, I'm having a reflection brain fog here, perhaps someone can set me on the right track. I'd like to define a custom attribute to be used in a class hierarchy. What I want to do is to...
3
by: =?Utf-8?B?Sm9hcXVpbg==?= | last post by:
OK, this was hard to explain on a short title. Sorry. I have an abstract class (MustInherit) named Base, and a generic collection class named ColBase which can only take Base and derived classes...
6
by: Immortal Nephi | last post by:
First class is the base class. It has two data: m_Base1 and m_Base2. Second class and third class are derived classes and they are derived from first class. m_Base1 and m_Base2 are inherited into...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.