@@ -281,13 +281,51 @@ <h2>Practical Advice</h2>
281281</ ul >
282282
283283< hr >
284+ < h2 > Light-weight alternative: pickle support implemented in Python</ h2 >
284285
285- © Copyright Ralf W. Grosse-Kunstleve 20012-2002. Permission to copy,
286+ < h3 > < a href ="../../test/pickle4.cpp "> < tt > pickle4.cpp</ tt > </ a > </ h3 >
287+
288+ The < tt > pickle4.cpp</ tt > example demonstrates an alternative technique
289+ for implementing pickle support. First we direct Boost.Python via
290+ the < tt > class_::enable_pickling()</ tt > member function to define only
291+ the basic attributes required for pickling:
292+
293+ < pre >
294+ class_<world>("world", args<const std::string&>())
295+ // ...
296+ .enable_pickling()
297+ // ...
298+ </ pre >
299+
300+ This enables the standard Python pickle interface as described
301+ in the Python documentation. By "injecting" a
302+ < tt > __getinitargs__</ tt > method into the definition of the wrapped
303+ class we make all instances pickleable:
304+
305+ < pre >
306+ # import the wrapped world class
307+ from pickle4_ext import world
308+
309+ # definition of __getinitargs__
310+ def world_getinitargs(self):
311+ return (self.get_country(),)
312+
313+ # now inject __getinitargs__ (Python is a dynamic language!)
314+ world.__getinitargs__ = world_getinitargs
315+ </ pre >
316+
317+ See also the
318+ < a href ="../tutorial/doc/extending_wrapped_objects_in_python.html "
319+ > tutorial section</ a > on injecting additional methods from Python.
320+
321+ < hr >
322+
323+ © Copyright Ralf W. Grosse-Kunstleve 2001-2004. Permission to copy,
286324use, modify, sell and distribute this document is granted provided this
287325copyright notice appears in all copies. This document is provided "as
288326is" without express or implied warranty, and with no claim as to its
289327suitability for any purpose.
290328
291329< p >
292- Updated: Aug 2002 .
330+ Updated: Feb 2004 .
293331</ div >
0 commit comments