0

This question was marked as duplicate of another question. I am not asking how to create enumerations, I'm asking how to create constants, and that is the difference between the questions.

This question was marked as duplicate of yet another question. While the answers do explain how to create constants in python they don't explain how to create them in Enum like style, which is what my question is asking for.

I need to create constants in my python code. I don't want the user to be able to modify them. I found this, but it is not exactly what I need. I need to be be able to use them just like enums, without creating and instance.

For example:

class MyConstClass(object):
    Const0 = 0
    Const1 = 1
    Const2 = 2

myfunc(MyConstClass.Const0)
MyConstClass.Const1 = 20 # Raise an exception

I also don't want to create a module, just like it is done here

Is there a way to do it?

Community
  • 1
  • 1
flashburn
  • 3,290
  • 3
  • 39
  • 76
  • 2
    In Python, *everything* can be modified. Rather than try to lock this down, trust the developer. – Martijn Pieters Nov 08 '16 at 22:36
  • "I need to create constants in my python code. I don't want the user to be able to modify them." - essentially impossible, and not worth trying. – user2357112 supports Monica Nov 08 '16 at 22:36
  • http://code.activestate.com/recipes/65207-constants-in-python/?in=user-97991 – davedwards Nov 08 '16 at 22:41
  • If you really want and/or must do that, you shouldn't be using Python at all, as this goes against Python ideology. That's not Pythonic at all. – JChris Nov 08 '16 at 23:02
  • @MartijnPieters I completely understand everybody's comments about not using constants and Python. I was aware of it when I was posting the question. However I'm working on a larger project and the language is locked in, it cannot change. There is also a need to have constants. Assumption is that people using this project do not have sufficient Python knowledge to actually modify constants. I can't go into details, but it is a reasonable assumption. – flashburn Nov 09 '16 at 00:50
  • @user2357112 See my comment above – flashburn Nov 09 '16 at 00:50
  • @Juan See my comment above – flashburn Nov 09 '16 at 00:50
  • @zvone Please see my edited version of the question – flashburn Nov 09 '16 at 00:54
  • @wim Please see my edited version of the question – flashburn Nov 09 '16 at 00:54
  • You'll have to distinguish your question from the duplicates if you want it to be re-opened. Specifically, what is it about simply swapping the base class `object` in your code snippet for `enum.Enum` that is not suitable for you? (note: you will have to `pip install enum34` backport for python 2.x) – wim Nov 09 '16 at 01:01
  • @wim The difference is philosophical. Enumrations are meant to be changed later (at least in my view). I usually use enumerations in code where value of enumeration doesn't matter. However the value of the constants does not change. Bit masks could be bit masks, it doesn't make sense to use Enumrations for this. – flashburn Nov 09 '16 at 01:38
  • 2
    Philosophical questions are off-topic on stackoverflow. An `Enum` is suitable for the use-case you've described. – wim Nov 09 '16 at 01:49

0 Answers0