[C++-sig] Creating new instances of an object's contained value

Stefan Seefeld seefeld at sympatico.ca
Mon Dec 5 07:16:52 CET 2005


Matt wrote:
> Hey all,
> 
> This is probably a newbie question, but I'm curious if it's possible to 
> create a new instance of an object's contained value into another object 
> variable in an extension module I'm working on.
> 
> For example, I am trying to create a factory method of sorts that will 
> create a python object and return it.  Rather than load up the dictionary 
> and module and call Py_RunString each time the method is called, I would 
> like to only do this once (I'm not worried about reloading this python 
> object after the extension module is loaded).  Thus, I was thinking of 
> keeping an object variable that I can "clone", so to speak, and return the 
> cloned value.  Is this possible?

Python has a 'copy' module that may do what you want. However, there might
be a far simpler way to achieve the same thing:

In Python, types are objects, too. So, your factory may read in the python
script you are referring to once, and then extract the type objects which
the parser instantiates from the global dictionary.
Then, each time you call 'factory->create()' (or however it is spelled),
you simply call operator() on the appropriate type object (with suitable arguments)
and return the result.

Would that work for you ?

HTH,
		Stefan



More information about the Cplusplus-sig mailing list