2

PEP 8 prescribes that

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

I understand that this is a naming convention only, but I'm curious to know if there is an official or widely-accepted definition of what actually constitutes a constant versus a semi-private variable.

Is this limited to what you might call mathematical constants? (Namely float, int, complex)

Or would it include things like datetime.date objects or strings that are defined at the module level? How about a dictionary with keys/values of mixed types? If the answer is no, should these types of objects be defined like (quasi-) private variables with _leading_underscore?

6
  • Python does not actually have real constants. The naming convention is just there for readability. Commented Sep 1, 2017 at 16:32
  • Essentially, since Python does not have actual "constants", uppercasing some module-level object (not limited to mathematical constants) simply signals to those reading your code "don't mess with this, this shouldn't change" Commented Sep 1, 2017 at 16:32
  • As in all programing languages a constant is just something that does not change. Commented Sep 1, 2017 at 16:36
  • Here is a link to a question about the use of constants. the-benefits-of-constants. In other languages it has a benefit but in python its just a way of saying "DONT CHANGE THIS" Commented Sep 1, 2017 at 16:38
  • 2
    @BradSolomon: Well, a constant is expected to be constant. "Private" and "constant" are entirely independent concepts, and you could have something like _FOO for a private constant. Commented Sep 1, 2017 at 16:43

2 Answers 2

7

Well, the PEP 8 coding conventions were primarily written for the Python standard library:

This document gives coding conventions for the Python code comprising the standard library in the main Python distribution.

Grepping through some of the modules in the Python 3.5 standard library, alongside with the usual strings and numbers, one finds things like

UUIDs, uuid.py:

NAMESPACE_URL = UUID('6ba7b811-9dad-11d1-80b4-00c04fd430c8') 

regular expressions, smtplib.py

OLDSTYLE_AUTH = re.compile(r"auth=(.*)", re.I) 

dictionaries, plistlib.py:

_BINARY_FORMAT = {1: 'B', 2: 'H', 4: 'L', 8: 'Q'} 

frozensets, asyncore.py:

_DISCONNECTED = frozenset({ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED, EPIPE, ... 

and a datetime.date in calendar.py

_EPOCH_ORD = datetime.date(EPOCH, 1, 1).toordinal() 

thus basically anything that is supposed to remain constant for the entire duration of the execution, even if it were a mutable type (the dictionary in plistlib.py).

Sign up to request clarification or add additional context in comments.

Comments

1

For the most part in python the PEP8 naming convention for constants is just there to help any programers who are reading the code know not to change that particular "constant" In other languages like C it actually can reduce the time it takes to execute code.

See this post on The Benefits of Constants.

A constant is just a value that never changes.

In OOP, it is useful to declare private or public constants but for python there is no functional separation from "Constants" and regular variables.

One more thing in other programming languages that support true constants if you declare a value as a constant then that value cannot change not even if you try to reassign that value later. In Python, however, there are no true constants, so we use the naming convention to help us remember not to change that value.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.