zfranciscus zfranciscus - 9 days ago 4x
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?


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


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