17
tahnik
7y

So python does not have constants and the only way to create one is to create a class with a getter but no setter.

WHAT.THE.FUCK or am I just wrong?

Comments
  • 7
    The philosophy of Python is something around “We are all adults here”, so this is why there no way to block something, and the class property thing is not making it private, as again, the philosophy, you can access any variable like this

    test_class.property

    The convention to make something a “constant”, to indicate to people not to change it (but if they ultimately need to, they can) is to SCREAMING_SNAKE_CASE it (I love to write it):

    MY_NAME = “[redacted]”

    To make a “private” class property, add an underscore:

    _example_class_property = “you ain’t suppose to access this”
  • 10
    @RedBorg I guess the creator of python never worked in a workplace full of noobs.
  • 4
    @RedBorg A constant is called a constant because its value is constant, and not changeable. If it's intended to be changed at runtime, it's not a constant.
    Constants may change over time (aka between builds), therefore it's a good idea to declare it only once and use its name several times, but never at runtime.

    And it's merely a comfort thing to give constants name and not just put magic numbers/literals everywhere (which would be a code smell BTW).

    And it's always a good idea to restrict access to something as good as possible, because if something can go wrong it will go wrong. That may not happen on purpose, but at the end it doesn't matter if the cause of a bug was created intentionally or by accident.
  • 1
    @ddephor That’s why I put the “, but the concept of python is you do what you want with the code, so if kiddies change the “constants” (marked in screaming snake case), they face the consequences, but if you’re there to be serious, you won’t go change a “constant”. That’s why it’s “We are all ADULTS here”, but I understand the comfort zone that it creates as I learned programming with Java. It’s just another way to think and code.
Add Comment