Is there no const constant keyword in Python?

Asked 2 years ago, Updated 2 years ago, 113 views

How do I specify variables as constants in Python?

In Java, public static final String CONST_NAME = "Name"; I wrote it the same way

Const, Const CONST, Final, Final, Final, and Final are all not allowed in Python. How can I declare a constant?

python const

2022-09-22 22:28

1 Answers

Python has no constant keyword, so there is no constant declaration. Instead, there is a rule that names of constants are all capitalized and underlined (_), which warns you if most editors assign something to a variable in an uppercase name.

Java's public static final string CONST_NAME = "Name"; is replaced by Python.

class Foo(object):
    CONST_NAME = "Name"

#or
CONST_NAME = "Name"

This is.


2022-09-22 22:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.