No there is not.
(不,那里没有。)
You cannot declare a variable or value as constant in Python. (您无法在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
(如果不是,那只是)
CONST_NAME = "Name"
But you might want to have a look at the code snippet Constants in Python by Alex Martelli.
(但是您可能想看看Alex Martelli编写的Python中的代码片段Constants 。)
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.
(从Python 3.8开始,有一个typing.Final
变量注释将告诉静态类型检查器(如mypy)不应重新分配您的变量。)
This is the closest equivalent to Java's final
. (这与Java的final
最接近。)
However, it does not actually prevent reassignment : (但是,它实际上并不能阻止重新分配 :)
from typing import Final
a: Final = 1
# Executes fine, but mypy will report an error if you run mypy on this:
a = 2
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…