473,432 Members | 1,624 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,432 software developers and data experts.

Class data members in C


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
5 1459
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
"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

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
"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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Carlos Ribeiro | last post by:
I thought about this problem over the weekend, after long hours of hacking some metaclasses to allow me to express some real case data structures as Python classes. I think that this is something...
5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
6
by: lovecreatesbeauty | last post by:
Hello Experts, Why static data members can be declared as the type of class which it belongs to? Inside a class, non-static data members such as pointers and references can be declared as...
5
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
16
by: Crirus | last post by:
I have a class. I need to write a routine in this class, that loop through it's members (in a instance of the class) and concatenate all members values as string. I need to filter does members...
2
by: Jason | last post by:
Hello: First, if this is one of those "questions asked a million times" just say so and I'll dig a little deeper. If not, then... I'm curious, is it typical to use member data (properties,...
14
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.