When should a variable be set as a private class variable (e.g. _var) against a class constant (e.g. VAR) versus a private class constant (e.g. _VAR)?

I find myself unsure whether I should set certain variables that I use in my class, like private class variables (e.g. _var) and class constant variables (e.g. VAR) and a constant class variable if such a thing (e.g. _VAR). I understand that this does not really matter in Python other than the convention, but I would like to know which path is right (or more right).

For example, let's say I have a specific variable to hold a regular expression pattern for height. Let's say I have no intention of modifying this anywhere in the class or elsewhere in the code, and in fact I use it only in one of the methods of the class I should go with:

Option 1 - set as a private class variable:

_height_pattern = r"""(#'##?"?)|#'"""

Option 2 - Defined as a constant class variable

HEIGHT_PATTERN = r"""(#'##?"?)|#'"""

Option 3 - set as a private class constant variable (not sure if such a thing exists or have I ever seen a variable declared in this for)

_height_pattern = r"""(#'##?"?)|#'"""

, , - , . , , , , . , , . , , (_SEED = 2000?), ? , , .

+4
2

, . .

, , , , , , , . , 3 (, , ), 2, , , , , .

+1

pep-0008 . HEIGHT_PATTERN.

, "style" python pep 8 (. )

0

Source: https://habr.com/ru/post/1661977/


All Articles