1. 12

  2. 3

    final entity is something that can not be changed.

    Well, it’s probably more accurate to say that immutable things are ones that cannot be changed. final names in Python are ones that can’t (shouldn’t be) assigned to. In this case DAYS_IN_A_WEEK is referring to an immutable int. But it could just as well be an object with methods that mutate underlying members. So it’s great to pair final with immutable things like tuples or namedtuples (or attrs )

    1. 3

      It used to be a time when various complicated attempts at implementing, say, Singleton pattern in Python was considered to be a laughing matter. Python way was always to not invent a problem when common sense and evidence show that there isn’t any. This is the same case. There was never any problem with Python constants-by-convention not actually being constants.

      1. 1

        It needs an external tool to validate your code? That’s not very convincing to me. How is this going to work with runtime generated code?

        1. 4

          That’s how Python typing is. Python is dynamically typed, but the devs are working on adding gradual typing via syntax extensions and a static type checker (mypy), but they’ve stated that typing will always be optional and not cover every case.

          1. 2

            It needs an external tool to validate your code?

            It’s the canonical type checking feature for Python. It leverages the language standard for type hints. One could imagine that perhaps CPython or other interpreters could eventually gain a feature to execute these type checks. Though I suspect that it would still be an independent execution/pass, as it is with mypy.

            How is this going to work with runtime generated code?

            Does this mean that you have a use case for generating python source code at runtime, or something else? If you are generating python source, you could add a step to send it to mypy first, if you were so inclined.

          2. 1

            s/real constants/constant type/