0

like in PHP you can do

class C { private const FOO=123; } 

for constants that are only relevant inside the class/not part of the public API, does Python support something similar?

6
  • And you can use the @property method decorator to make the value read only (i.e. a constant), @property def __foo(self): return 123 Commented Mar 31, 2022 at 9:15
  • 1
    I think stackoverflow.com/questions/1641219/… answers the "private" part, for constants there is mypy.readthedocs.io/en/stable/final_attrs.html ...in both cases it is not enforced by the runtime, it's convention (and in case of mypy, optional type checking) only Commented Mar 31, 2022 at 9:15
  • 1
    @property (i.e. a getter without a setter) is not the same as const, though for many use cases the behaviour will be similar Commented Mar 31, 2022 at 9:18
  • I'm not implying that it is the same, but that it will allow you to emulate it in a slightly pythonic way. __ isn't really private either, so you can always get around these limitations in some way. Commented Mar 31, 2022 at 9:27
  • totally agree :) Commented Mar 31, 2022 at 9:30

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.