zfranciscus zfranciscus - 1 month ago 6
Python Question

How to create a constant in Python

Is there a way to declare a constant in Python. In Java we can create constant in this manner:

public static final String CONST_NAME = "Name";


What is the equivalent of the above Java constant declaration in Python?

Answer

No there is not. You cannot declare a variable or value as constant in Python. Just don't change it.

If you are in a class, the equivalent would be:

class Foo(object):
    CONST_NAME = "Name"

if not, it is just

CONST_NAME = "Name"

But you might want to have a look at the code snippet Constants in Python by Alex Martelli.