Answer after OP's edit
A literal constant is an actual literal value; I know the word literal confuses you but an example might make it clearer. If you type the following in the REPL:
>>> 2
2
>>> 'hello'
'hello'
2
and hello
are actual literal constants and contrary to what you think, you can't change their value (well, you can, as a beginner, it's best to not know about that). When you have:
stack = 2
stack = 3
You are first assigning the constant literal (though, honestly, don't worry about what it's called, it's the number 2) to stack
. So, the name stack
is pointing to the value 2
. Then, by saying stack = 3
, you are not changing the value 2
; you are now making the name stack
to point to another value, 3
.
For what it's worth, "constant literal" sounds complicated; just think of values like 2
or 'John'
etc. as what they are. And with regards to actual constants (in programming constants are referred to variables that cannot be changed after assignment), that concept doesn't really exist in Python. A constant is when, for instance, you say stack = 2
but then you cannot ever change what stack
is pointing to or you'll get an error. In Python, this concept does not exist.
Original Answer:
For starters, I recommend you read The story of None, True and False (and an explanation of literals, keywords and builtins thrown in) by Guido:
A literal, on the other hand, is an element of an expression that describes a constant value. Examples of literals are numbers (e.g. 42, 3.14, or 1.6e-10) and strings (e.g. "Hello, world"). Literals are recognized by the parser, and the exact rules for how literals are parsed are often quite subtle.
As for "constants", you cannot declare a variables as "true constants" in Python. There are a Built-in Constants like True
and False
and None
in Python but even they are not"true constants" in Python 2.X as they can be assigned to point to another value:
True = False
if True:
print 'Hey'
else:
print 'WAAAT!'
I hope this helps. If not, please edit your questions and give an example of what you mean exactly by Constants and Literal Constants.
Note: True
and False
and the like are keywords in Python 3.x, so if you say True = False
, the interpreter will raise SyntaxError: assignment to keyword
.
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
UUID
s, 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'}
frozenset
s, 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).
Best Answer
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:
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'sfinal
. However, it does not actually prevent reassignment: