Re: [capi-sig] Dynamically add members to a object at runtime
On 20.11.2012 16:23, Robert Steckroth wrote:
Hello Gang, I have come across the need to dynamically add members to an object at runtime. I would like to attach many objects of a single separate type into a object for processing. Is there a way to dynamically create object members while maintaining the (de)allocation structure? It would be nice to create an array of objects in a single PyObject but then a Py_DECREF() would not work on all of the objects in the array. Has anyone come across the need for this? It would make the module I am creating very easy to use. If not, a short "no" will not hurt my feelings ,')
E.g. w = DateTime() x = DateTime() my_object.add(w) my_object.add(x)
And then have some internal stuff possess those objects, E.g. my_object.combine_dates()
Dynamically adding members is possible, but requires extra work.
I'd simply stick all the extra objects into a dictionary, have the object reference (and hold the reference to) the dictionary and release it when the object is deallocated.
If you want garbage collection to work on your object, you will also have to implement the traversal API, so that cyclic references can be dealt with by the Python GC.
-- Marc-Andre Lemburg eGenix.com
Professional Python Services directly from the Source (#1, Nov 21 2012)
Python Projects, Consulting and Support ... http://www.egenix.com/ mxODBC.Zope/Plone.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
::: Try our new mxODBC.Connect Python Database Interface for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/
participants (1)
-
M.-A. Lemburg