473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Adding and attribute to an instance

J
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

Aug 9 '05 #1
3 1775
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


Aug 10 '05 #2
J wrote:
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.


I asked this a few months ago......

Basically, you need a PyObject * in your object record:

struct PyStruct {
PY_OBJECT_HEAD
// ...
PyObject *dict;
// ....
}
then add the offset to the tp_dictoffset member in the type struct:

PyTypeObject PyType {
// ....
offsetof(PyStruct, dict), /* tp_dictoffset */
// ...

Make sure you init this member to 0 (tp_init), and make sure you
PyXDECREF() it when the object is deleted (tp_dealloc).

Optionally: add a __dict__ entry to the PyMemberDefs so that
obj.__dict__ works (tho this is not necessary):

{"__dict__", T_OBJECT, offsetof(PyStruct, dict), READONLY },

The ROOOOOOLY cool bit: You don't need to add the dictionary with
PyDict_New() etc, because the python runtime will create this dict the
first time a new attrribute is added. So the cost of this feature for
object instances that don't have extra attributes is just 4 unused bytes
in the instance record! This is just sooooooo cool.
Greg.
Aug 10 '05 #3
Gregory Bond wrote:
Make sure you init this member to 0 (tp_init), and make sure you
PyXDECREF() it when the object is deleted (tp_dealloc).


As this may cause your objects to appear in cycles, you may also
have to add support for cyclic GC (unless you already did this
before, and unless you can somehow rule out that your objects
are in cycles).

Regards,
Martin
Aug 10 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gabriel Genellina | last post by:
Hi In the following code sample, I have: - a Worker class, which could have a lot of methods and attributes. In particular, it has a 'bar' attribute. This class can be modified as needed. - a...
7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
2
by: Jordan Willms | last post by:
Hi there. I am working with lom metadata and I am a little confused with how to form the following xml element: <lom xmlns="http://www.imsglobal.org/xsd/imsmd_v1p2"...
11
by: Steven D'Aprano | last post by:
Suppose I create a class with some methods: py> class C: .... def spam(self, x): .... print "spam " * x .... def ham(self, x): .... print "ham * %s" % x .......
4
by: glebur | last post by:
Hi, I'm trying to create a web service client in C# but I get stuck at one of the first steps. When adding a Web reference to the Visual Studio project; I get this error (this is a translation,...
3
by: Andy | last post by:
Hello Guys: What am I doing wrong with this code? I can't seem to get it to simply add an attribute to my node. The node already exists. I am simply opening the XMLDocument and creating one...
9
by: Mike | last post by:
I was messing around with adding methods to a class instance at runtime and saw the usual code one finds online for this. All the examples I saw say, of course, to make sure that for your method...
3
by: zslevi | last post by:
Can I access the class attributes from a method added at runtime? (My experience says no.) I experimented with the following code: class myclass(object): myattr = "myattr" instance =...
18
by: Gabriel Rossetti | last post by:
Hello everyone, I had read somewhere that it is preferred to use self.__class__.attribute over ClassName.attribute to access class (aka static) attributes. I had done this and it seamed to work,...
11
by: =?Utf-8?B?TTFpUw==?= | last post by:
I’m trying to add the following attributes (xmlns:xsi and xsi:type) to a node (Grantee) like the following: <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.