zfranciscus zfranciscus - 3 years ago 201
Python Question

How do I create a constant 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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download