what is the best way to define constant variables python 3 [duplicate]

Constants (in a sense) in Python 3.8+

Python 3.8 introduces the typing.Final type qualifier, which is used to indicate that a variable or attribute should not be reassigned, redefined, or overridden.

PEP 591 — Adding a final qualifier to typing

from typing import Final

# Annotate module variables
# (with or without an explicit type, using the syntax Final[<type>])
# (type is auto-determined in absence of an explicit type)
PI: Final[float] = 3.141592654
ANSWER_TO_EVERYTHING: Final = 42


# Annotate instance variables in class bodies
# (explicit type is needed if no value is assigned)
class Point:
    x: Final[int]
    y: Final = 0

    def __init__(self, x: int):
        self.x = x


# Annotate instance variables directly
# (only allowed in __init__ methods)
class Person:
    def __init__(self, birth_year: int):
        self.birth_year: Final = birth_year

Linters and type checkers will show you warnings if you reassign or redefine Final variables. Note that there is no runtime check, so you can still run the code below.

ANSWER_TO_EVERYTHING: Final = 42
ANSWER_TO_EVERYTHING = 420  # shows warning
print(ANSWER_TO_EVERYTHING)  # prints 420

There is also the typing.final decorator, which is used to restrict inheriting classes and overriding methods.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)