By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,922 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,922 IT Pros & Developers. It's quick & easy.

Class data members in C

P: n/a

Hmm. The extensions documentation describes how to add instance
members to a class (PyMemberDef), but I want to add a class member.
Yes, this is constant for all instances of the class.

Any pointers?
Regards,
Nick Maclaren.
Jan 16 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Nick Maclaren wrote:
Hmm. The extensions documentation describes how to add instance
members to a class (PyMemberDef), but I want to add a class member.
Yes, this is constant for all instances of the class.

Any pointers?
Add something like this to your PyMODINIT_FUNC after you have
initialized your type with PyType_Ready:

PyDictSetItemString(YourType.tp_dict, "attrname", attrvalue);

Ziga

Jan 16 '07 #2

P: n/a
"Nick Maclaren" <nm**@cus.cam.ac.ukwrote:

>
Hmm. The extensions documentation describes how to add instance
members to a class (PyMemberDef), but I want to add a class member.
Yes, this is constant for all instances of the class.

Any pointers?
When? - at time of defining base class, between class definition and
first instance, at time of creating instance, or after instance creation?

After instance creation is not easy, I think you would have to add
to each instance...

But then, I may be wrong - I often am.

- Hendrik

Jan 17 '07 #3

P: n/a

In article <ma***************************************@python. org>,
"Hendrik van Rooyen" <ma**@microcorp.co.zawrites:
|
| Hmm. The extensions documentation describes how to add instance
| members to a class (PyMemberDef), but I want to add a class member.
| Yes, this is constant for all instances of the class.
|>
|When? - at time of defining base class, between class definition and
|first instance, at time of creating instance, or after instance creation?
|>
|After instance creation is not easy, I think you would have to add
|to each instance...

Oh, one of the first two - I am not bonkers! Changing a class after
instance creation is guaranteed to cause confusion, if nothing else.
Regards,
Nick Maclaren.
Jan 17 '07 #4

P: n/a
"Nick Maclaren" <nm**@cus.cam.ac.ukwrote:

>
In article <ma***************************************@python. org>,
"Hendrik van Rooyen" <ma**@microcorp.co.zawrites:
|
| Hmm. The extensions documentation describes how to add instance
| members to a class (PyMemberDef), but I want to add a class member.
| Yes, this is constant for all instances of the class.
|>
|When? - at time of defining base class, between class definition and
|first instance, at time of creating instance, or after instance creation?
|>
|After instance creation is not easy, I think you would have to add
|to each instance...

Oh, one of the first two - I am not bonkers! Changing a class after
instance creation is guaranteed to cause confusion, if nothing else.
*grin* - its also just about impossible to do, if you try to imagine
what you would have to do to all instances when you effectively
change the __init__ method. And some people expect it to happen
automagically...

Sorry I can't be of more help. All of the docs I have read essentially
talk about changing instances. From a practical point of view I would
say: "Just redefine it". But that is not very helpful. Specially if you are
mucking about in C, as your title suggests...

<off topic language note>

It has never struck me before that the root word for
"madness" and the slang for "copulation" is the same.
I wonder if there is some sort of a message here?

<end>

- Hendrik

Jan 18 '07 #5

P: n/a

In article <ma***************************************@python. org>,
"Hendrik van Rooyen" <ma**@microcorp.co.zawrites:
|>
| Oh, one of the first two - I am not bonkers! Changing a class after
| instance creation is guaranteed to cause confusion, if nothing else.
|
|*grin* - its also just about impossible to do, if you try to imagine
|what you would have to do to all instances when you effectively
|change the __init__ method. And some people expect it to happen
|automagically...

Oh, it's quite easy to do, with several different semantics. I have even
used a couple of them. All are seriously counter-intuitive :-)

I have never used one that successfully reran initialisation, though
I have seen people attempt it! And tried it myself ....

|Sorry I can't be of more help. All of the docs I have read essentially
|talk about changing instances. From a practical point of view I would
|say: "Just redefine it". But that is not very helpful. Specially if you are
|mucking about in C, as your title suggests...

Thanks. As someone else posted, the answer is PyDict_SetItemString
immediately after PyType_Ready.
Regards,
Nick Maclaren.
Jan 18 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.