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?
There's no perfect way to do this. As I understand it most programmers will just capitalize the identifier, so PI = 3.142 can be readily understood to be a constant.
On the otherhand, if you want something that actually acts like a constant, I'm not sure you'll find it. With anything you do there will always be some way of editing the "constant" so it won't really be a constant. Here's a very simple, dirty example:
This looks like it will make a PHP-style constant.
In reality all it takes for someone to change the value is this:
This is the same for all the other solutions you'll find on here - even the clever ones that make a class and redefine the set attribute method - there will always be a way around them. That's just how Python is.
My recommendation is to just avoid all the hassle and just capitalize your identifiers. It wouldn't really be a proper constant but then again nothing would.
Python dictionaries are mutable, so they don't seem like a good way to declare constants:
Maybe pconst library will help you (github).
$ pip install pconst
[Out] Constant value of "APPLE_PRICE" is not editable.
There's no
const
keyword as in other languages, however it is possible to create a Property that has a "getter function" to read the data, but no "setter function" to re-write the data. This essentially protects the identifier from being changed.Here is an alternative implementation using class property:
Note that the code is far from easy for a reader wondering about constants. See explanation below
Code Explanation:
constant
that takes an expression, and uses it to construct a "getter" - a function that solely returns the value of the expression.constant
function we just created as a decoration to quickly define read-only properties.And in some other more old-fashioned way:
(The code is quite tricky, more explanations below)
Note that the @apply decorator seems to be deprecated.
property
function to construct an object that can be "set" or "get".property
function's first two parameters are namedfset
andfget
.property
functionIn Python instead of language enforcing something, people use naming conventions e.g
__method
for private methods and using_method
for protected methods.So in same manner you can simply declare the constant as all caps e.g.
If you want that this constant never changes, you can hook into attribute access and do tricks, but a simpler approach is to declare a function
Only problem is everywhere you will have to do MY_CONSTANT(), but again
MY_CONSTANT = "one"
is the correct way in python(usually).You can also use namedtuple to create constants:
Edit: Added sample code for Python 3
Note: this other answer looks like it provides a much more complete implementation similar to the following (with more features).
First, make a metaclass:
This prevents statics properties from being changed. Then make another class that uses that metaclass:
Or, if you're using Python 3:
This should prevent instance props from being changed. To use it, inherit:
Now the props, accessed directly or via an instance, should be constant:
Here's an example of above in action. Here's another example for Python 3.