Adding and attribute to an instance
Jeremy Moles
jeremy at emperorlinux.com
Wed Aug 10 01:17:03 EDT 2005
Hmmm--I would also be interested in knowing the answer to this. I get
the exact same behavior as the OP (probably because it's the intended
behavior?)
On Tue, 2005-08-09 at 14:38 -0700, J wrote:
> Hi,
>
>
> I am trying to add new data attributes to my extension classes from
> within a script. I am under the impression that python allows
> that implicity
>
>
> This is the definition of my class
>
> PyTypeObject CmdPlace::PyType =
> {
> PyObject_HEAD_INIT(NULL)
> 0, /*ob_size*/
> "Place", /*tp_name*/
> sizeof(CmdPlace::PyStruct), /*tp_basicsize*/
> 0, /*tp_itemsize*/
> 0, /*tp_dealloc*/
> 0, /*tp_print*/
> 0, /*tp_getattr*/
> 0, /*tp_setattr*/
> 0, /*tp_compare*/
> 0, /*tp_repr*/
> 0, /*tp_as_number*/
> 0, /*tp_as_sequence*/
> 0, /*tp_as_mapping*/
> 0, /*tp_hash */
> 0, /*tp_call*/
> 0, /*tp_str*/
> PyObject_GenericGetAttr, /*tp_getattro*/
> PyObject_GenericSetAttr, /*tp_setattro*/
> 0, /*tp_as_buffer*/
> Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
> "CmdPlace", /* tp_doc */
> 0, /* tp_traverse */
> 0, /* tp_clear */
> 0, /* tp_richcompare */
> 0, /* tp_weaklistoffset */
> 0, /* tp_iter */
> 0, /* tp_iternext */
> CmdPlace::sPyMethods, /* tp_methods */
> CmdPlace::sPyMembers, /* tp_members */
> CmdPlace::sPyGetSeters, /* tp_getset */
> 0, /* tp_base */
> 0, /* tp_dict */
> 0, /* tp_descr_get */
> 0, /* tp_descr_set */
> 0, /* tp_dictoffset */
> 0, /* tp_init */
> 0, /* tp_alloc */
> 0, /* tp_new */
> };
>
> I call
>
> PyType_Ready(&PyType);
> Py_INCREF(&PyType);
>
> to initialize the type, and
>
> PyObject_INIT((PyObject*)&mPyObject, &CmdPlace::PyType);
>
> to initialize an object. Objects of this type are only ever
> instantiated from C++. When I evaluate a sript I just add the object as
> "MyObject" to the dicitonary passed to Py_Eval... All the
> members and methods work fine, but when I do
>
> MyObject.aNewAttribue = 12
>
> I get at an error saying
>
> object has no attribute "aNewAttribue".
>
> I have looked at some of the source code in PyObject_GenericGetAttr and
> it turns out that the object has no dictionary. It seens that the
> address of the dictionary is computed somehow via tp_dictoffset in the
> type object.
>
> Basically my question is, how can I make this work.
>
>
> Cheers
> Jochen
>
More information about the Python-list
mailing list