Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constant integer Attributes in Python Module written in C

I implemented a python extension module in C according to https://docs.python.org/3.3/extending/extending.html

Now I want to have integer constants in that module, so I did:

module= PyModule_Create(&myModuleDef);
...
PyModule_AddIntConstant(module, "VAR1",1);
PyModule_AddIntConstant(module, "VAR2",2);
...
return module;

This works. But I can modify the "constants" from python, like

import myModule
myModule.VAR1 = 10

I tried to overload __setattr__, but this function is not called upon assignment.

Is there a solution?

like image 667
MichaelW Avatar asked Jan 24 '26 23:01

MichaelW


1 Answers

You can't define module level "constants" in Python as you would in C(++). The Python way is to expect everyone to behave like responsible adults. If something is in all caps with underscores (like PEP 8 dictates), you shouldn't change it.

like image 142
StoryTeller - Unslander Monica Avatar answered Jan 26 '26 14:01

StoryTeller - Unslander Monica