MichaelW
MichaelW

Reputation: 1464

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?

Upvotes: 3

Views: 310

Answers (1)

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.

Upvotes: 6

Related Questions