[capi-sig] Dynamically add members to a object at runtime

Campbell Barton ideasman42 at gmail.com
Tue Nov 20 23:02:52 CET 2012


Internally you can use a list in your custom PyObject's struct, (or
possibly a tuple if you are sure its kept for internal use - since
you'd have to resize it).
if you do hold a reference to other PyObject's from a PyTypeObject you
define, be sure to fill in PyTypeObject.tp_traverse and include
Py_TPFLAGS_HAVE_GC in your tp_flags.
for example see:


Another option is to attach a PyObject* dict to your struct and define
PyTypeObject.tp_dictoffset
for example see:
https://svn.blender.org/svnroot/bf-blender/trunk/blender/source/blender/python/intern/bpy_library.c
lines, 72, 156

This has the advantage you can add whatever you like into your object
and name your members too, without messing with tp_traverse.

On Wed, Nov 21, 2012 at 2:23 AM, Robert Steckroth
<robertsteckroth at gmail.com> 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()
>
>
>
> --
> Bust0ut, Surgemcgee: Systems Engineer ---
> surgemcgee.com
> Django_Teamplate3d
> _______________________________________________
> capi-sig mailing list
> capi-sig at python.org
> http://mail.python.org/mailman/listinfo/capi-sig



-- 
- Campbell


More information about the capi-sig mailing list