Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Dev

How to pickle class derived from c++ extension

 

 

Python dev RSS feed   Index | Next | Previous | View Threaded


Martin.Drautzburg at web

Sep 14, 2007, 1:48 PM

Post #1 of 2 (255 views)
Permalink
How to pickle class derived from c++ extension

I understand that I can picke an extension class written in C/C++ by providing
a __reduce__() method along with __getstate__()/__setstate__(). While I still
havent gotten this to work, my main question is:

How could I possibly pickle an object of a python class which is derived from
the C++ extension?

It seems that I can define

>>> class Bar(list):
... pass

and add more attributes

>>> l=Bar()
>>> l.x=11

and __reduce__() will show the "x" attribute

>>> l.__reduce__()
(<function _reconstructor at 0xb7e2cf0c>, (<class '__main__.Bar'>,
<type 'list'>, []), {'x': 11})

But this does not seem to work with my extension class Foo. I defined a
__getstate__() method and __reduce__() indeed shows me some state. But if I
create a derived class Bar on the Python side and an object bar as an
instance of that class, and add an "x" attribute to that bar object, then
__reduce__ing that object shows nothing about the "x" attribute.

This is in a way undestandable, as __reduce__() eventually just calls
__getstate__() and the only implementation it can find is in my Foo extension
class, which knows nothing abpout the Bar derived class let alone its "x"
attribute.

I would like to have __reduce__() do it the pyhon way as far as it cat get,
and then call some magic method of my C++ class to pickle the "C++ part" of
an object. Is there a way to achieve this? The "list" class seems to have
something that my Foo class does not have. What is this?

Or of course if there is a better way, to picke objects of classes which are
derived from C++ extensions I'd be happy to hear about it.


_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


aahz at pythoncraft

Sep 15, 2007, 9:50 AM

Post #2 of 2 (217 views)
Permalink
Re: How to pickle class derived from c++ extension [In reply to]

On Fri, Sep 14, 2007, Martin Drautzburg wrote:
>
> I understand that I can picke an extension class written
> in C/C++ by providing a __reduce__() method along with
> __getstate__()/__setstate__(). While I still havent gotten this to
> work, my main question is:
>
> How could I possibly pickle an object of a python class which is
> derived from the C++ extension?

python-dev is not an appropriate place to ask questions about writing
your own applications. I suggest the C++-sig or capi-sig lists or the
comp.lang.python newsgroup.
--
Aahz (aahz [at] pythoncraft) <*> http://www.pythoncraft.com/

The best way to get information on Usenet is not to ask a question, but
to post the wrong information.
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com

Python dev RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.