9

How would one expose "static" variables like this

class MyClass:
    X = 1
    Y = 2

via the C API? The only variable on the PyTypeObject that looks like it would work is tp_members, but I see no flag in the PyMemberDef to indicate that the member should be per-class, not per-instance.

For a bit more clarification, since it may change the answer, I'm trying to expose a C enum to Python such that the enumeration

enum MyFlags {
    Alpha = 0,
    Beta = 1
};

Can be accessed in Python as:

module.MyFlags.Alpha
module.MyFlags.Beta
Toji
  • 33,927
  • 22
  • 105
  • 115
  • I only found this SO question after I had figured out the answer myself. Prior to that I searched for things like "cpython class constants". I wonder if we could add more words to this question that could help other people find this. – user1411349 Dec 01 '21 at 05:57

1 Answers1

12

Just put them in the type's tp_dict e.g. with PyDict_SetItemString.

user2357112
  • 260,549
  • 28
  • 431
  • 505
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • Excellent! That's what I was looking for. (You'll excuse me if I take a moment to test it out before marking this as answered.) – Toji Mar 04 '10 at 01:43
  • Works as advertised. :) Thanks again. – Toji Mar 04 '10 at 05:23
  • Since the time this answer was originally posted, the following warning has been added to the docs: "**Warning:** It is not safe to use PyDict_SetItem() on or otherwise modify tp_dict with the dictionary C-API." – user2357112 Jul 19 '20 at 21:42
  • 1
    I *think* that warning is a bit broader than it should be - it should be okay to set entries in `tp_dict` as long as the names don't have any corresponding slots (so no setting `__add__` or `__new__` or stuff like that) and you call `PyType_Modified` on the type afterward. – user2357112 Jul 19 '20 at 21:56