Is there a way to declare a constant in Python? In Java we can create constant values in this manner:
public static final String CONST_NAME = "Name";
What is the equivalent of the above Java constant declaration in Python?
Is there a way to declare a constant in Python? In Java we can create constant values in this manner:
public static final String CONST_NAME = "Name";
What is the equivalent of the above Java constant declaration in Python?
In python, a constant is simply a variable with a name in all capitals, with words separated by the underscore character,
e.g
DAYS_IN_WEEK = 7
The value is mutable, as in you can change it. But given the rules for the name tell you is a constant, why would you? I mean, it is your program after all!
This is the approach taken throughout python. There is no
private
keyword for the same reason. Prefix the name with an underscore and you know it is intended to be private. Code can break the rule....just as a programmer could remove the private keyword anyway.Python could have added a
const
keyword... but a programmer could remove keyword and then change the constant if they want to, but why do that? If you want to break the rule, you could change the rule anyway. But why bother to break the rule if the name makes the intention clear?Maybe there is some unit test where it makes sense to apply a change to value? To see what happens for an 8 day week even though in the real world the number of days in the week cannot be changed. If the language stopped you making an exception if there is just this one case you need to break the rule...you would then have to stop declaring it as a constant, even though it still is a constant in the application, and there is just this one test case that sees what happens if it is changed.
The all upper case name tells you it is intended to be a constant. That is what is important. Not a language forcing constraints on code you have the power to change anyway.
That is the philosophy of python.
You can use StringVar or IntVar, etc, your constant is const_val
As you probably already know, Python doesn't have constants :(
Perhaps the easiest alternative is to define a function for it. E.g.
MY_CONSTANT()
now has all the functionality of a constant (plus some annoying braces).I would make a class that overrides the
__setattr__
method of the base object class and wrap my constants with that, note that I'm using python 2.7:To wrap a string:
It's pretty simple, but if you want to use your constants the same as you would a non-constant object (without using constObj.value), it will be a bit more intensive. It's possible that this could cause problems, so it might be best to keep the
.value
to show and know that you are doing operations with constants (maybe not the most 'pythonic' way though).Simply you can just:
hope that makes everything much simpler
You can use a namedtuple as a workaround to effectively create a constant that works the same way as a static final variable in Java (a Java "constant"). As workarounds go, it's sort of elegant. (A more elegant approach would be to simply improve the Python language --- what sort of language lets you redefine
math.pi
? -- but I digress.)(As I write this, I realize another answer to this question mentioned namedtuple, but I'll continue here because I'll show a syntax that more closely parallels what you'd expect in Java, as there is no need to create a named type as namedtuple forces you to do.)
Following your example, you'll remember that in Java we must define the constant inside some class; because you didn't mention a class name, let's call it
Foo
. Here's the Java class:Here's the equivalent Python.
The key point I want to add here is that you don't need a separate
Foo
type (an "anonymous named tuple" would be nice, even though that sounds like an oxymoron), so we name our namedtuple_Foo
so that hopefully it won't escape to importing modules.The second point here is that we immediately create an instance of the nametuple, calling it
Foo
; there's no need to do this in a separate step (unless you want to). Now you can do what you can do in Java:But you can't assign to it:
Acknowledgement: I thought I invented the namedtuple approach, but then I see that someone else gave a similar (although less compact) answer. Then I also noticed What are "named tuples" in Python?, which points out that
sys.version_info
is now a namedtuple, so perhaps the Python standard library already came up with this idea much earlier.Note that unfortunately (this still being Python), you can erase the entire
Foo
assignment altogether:(facepalm)
But at least we're preventing the
Foo.CONST_NAME
value from being changed, and that's better than nothing. Good luck.