[C++-sig] PyCode_New and new.code() functions

Stefan Seefeld seefeld at sympatico.ca
Tue Sep 12 03:19:39 CEST 2006


anders langlands wrote:
> Thanks stefan, that does indeed look pretty simple!
> 
> However, is there any way I can do this with having my own interpreter?
> 
> The problem I am trying to solve is this:
> 
> I have a large class library I want to bind to python. Everything in the
> library (Derived) is derived ultimately from a single class, Base. Now I
> have functions to serialize and deserialize objects to disk. The
> deserialize
> function returns a Derived object as a Base*. When this Base* is passed
> back
> to python, python doesn't know that it's actually a Derived object, and it
> would be nice if it did. I basically want to be able to automatically cast
> the Base object to whichever flavour of Derived object it actually is in
> python.
> 
> No all my objects support the functions typeId() and typename() to get
> information about what they are. So the solution I've come up with is this.
> I just store a big dict or list in the globals which maps typeId's to
> functions. These functions do the conversion of the base type to the
> derived
> type and the whole thing looks something like this:

I'm not sure I understand the purpose of your downcasting. Those derived
python classes don't offer any additional attributes over what 'Base'
already has, or do they ?
If not, what advantage do you get from having the type names differ,
instead of using your own type system (provided by 'typeId()' and 'typename()' ?

If you really want to generate python types for the many C++ classes,
and you can do the conversion at runtime (as opposed to compile-time),
you may consider doing all of this from within a python module, by means
of the 'type()' constructor.

But again, I don't yet understand what you are aiming at, since that
won't give you anything you couldn't get by directly querying an object's
typeId().

Thanks,
		Stefan

-- 

      ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list