How to Create a Constant in Python

How do I create a constant in Python?

You cannot declare a variable or value as constant in Python.


To indicate to programmers that a variable is a constant, one usually writes it in upper case:

CONST_NAME = "Name"

To raise exceptions when constants are changed, see Constants in Python by Alex Martelli. Note that this is not commonly used in practice.


As of Python 3.8, there's a typing.Final variable annotation that will tell static type checkers (like mypy) that your variable shouldn't be reassigned. This is the closest equivalent to Java's final. However, it does not actually prevent reassignment:

from typing import Final

a: Final[int] = 1

# Executes fine, but mypy will report an error if you run mypy on this:
a = 2

How do I create a constant in Python?

You cannot declare a variable or value as constant in Python.


To indicate to programmers that a variable is a constant, one usually writes it in upper case:

CONST_NAME = "Name"

To raise exceptions when constants are changed, see Constants in Python by Alex Martelli. Note that this is not commonly used in practice.


As of Python 3.8, there's a typing.Final variable annotation that will tell static type checkers (like mypy) that your variable shouldn't be reassigned. This is the closest equivalent to Java's final. However, it does not actually prevent reassignment:

from typing import Final

a: Final[int] = 1

# Executes fine, but mypy will report an error if you run mypy on this:
a = 2

Overriding constants from parent class python

I believe PEP8 recommends using capital case for constants, but I usually interpret this as "module-level constants".

On the other hand, this is a class variable and PEP8 doesn't specify the naming for class variables.

But if we look at Google Style Guide for Python, here it clearly states that class constants should also be capitalized:

Global/Class Constants CAPS_WITH_UNDER

Naming constant class attribute in Python

Constants are usually defined on a module level and written in all capital letters with underscores separating words. Examples include MAX_OVERFLOW and TOTAL.

  • https://www.python.org/dev/peps/pep-0008/#constants

So, if it's a class attribute, it's not considered as a constant in terms of PEP-8.

However, there are few real-world exceptions.

Constants for Django models

In examples from Django documentation, constants for choices of fields are encapsulated into the model itself:

class Student(models.Model):
FRESHMAN = 'FR'
SOPHOMORE = 'SO'
JUNIOR = 'JR'
SENIOR = 'SR'
GRADUATE = 'GR'
YEAR_IN_SCHOOL_CHOICES = [
(FRESHMAN, 'Freshman'),
(SOPHOMORE, 'Sophomore'),
(JUNIOR, 'Junior'),
(SENIOR, 'Senior'),
(GRADUATE, 'Graduate'),
]
year_in_school = models.CharField(
max_length=2,
choices=YEAR_IN_SCHOOL_CHOICES,
default=FRESHMAN,
)

Source: Model field reference

Enums

enum.Enum uses SCREAMING_SNAKE_CASE for enum values. For example:

from enum import Enum

class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3

Source: enum.Enum documentation



Related Topics



Leave a reply



Submit