0

At a module level, is MY_VAR_C an anti-pattern in Python 3.8+?

MY_VAR_A = "my_var_a"
MY_VAR_B = "my_var_b"
MY_VAR_C: str = MY_VAR_A + MY_VAR_B
user1008636
  • 2,989
  • 11
  • 31
  • 45
  • 2
    I won't say so https://github.com/python/cpython/blob/main/Lib/string.py#L32 – azro May 15 '22 at 14:36
  • Opinionated. What are your actual concerns? – Ted Klein Bergman May 15 '22 at 14:39
  • @TedKleinBergman why does that example not use capital case? – user1008636 May 15 '22 at 14:40
  • Depends: do you use `MY_VAR_A` and `MY_VAR_B` for anything other than defining `MY_VAR_C`? – chepner May 15 '22 at 14:40
  • @user1008636 What do you mean? It does use capitals in its name. – Ted Klein Bergman May 15 '22 at 14:40
  • Yes, it's fine (my **opinion**, so not a good StackOverflow question). Also, If `MY_VAR_A` and `MY_VAR_B` are only used for `MY_VAR_C` or only in **that file** and nowhere else, then they should be named `_MY_VAR_A` and `_MY_VAR_B` with [leading underscores to indicate that it's sort of "private"](https://stackoverflow.com/q/1301346/1431750). Then readers (other coders) know to not use `A` and `B` in their code or imports, only to use `C`. – aneroid May 15 '22 at 14:57

1 Answers1

0

It is cheap and only executes once. So if it makes it easier to read it is a good trade off.

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164