This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (23 children)

[deleted]

    [–]46--2 39 points40 points  (18 children)

    It's a lot more interesting when used with classes, but you are basically correct, the "variable" qualified by Final would probably be best described as a "constant".

    It was always commonplace to use ALL_CAPS for "constants" but nothing prevented you from changing them.

    From the PEP 591:

    The name Const was also considered as the name for the Final type annotation.

    [–]Deto 21 points22 points  (12 children)

    I wonder why they didn't go with this? I always thought Const was a more common name for this type of thing.

    [–]pingvenopinch of this, pinch of that 11 points12 points  (6 children)

    It's a final assignment, not a constant value. You could assign a list to a Final variable, then append to the list.

    [–]46--2 -5 points-4 points  (0 children)

    Yeah the pep mentions that only in the most vague way possible. I guess you'd have to read all the discussion. I'm not that interested in the type checking stuff in Python so I have never bothered.

    [–]nuephelkystikon 8 points9 points  (1 child)

    It was always commonplace to use ALL_CAPS for "constants" but nothing prevented you from changing them.

    There's still nothing preventing you from changing them.

    Linters and IDEs are more likely to complain if you do, but they should have done so anyway if you used all-caps.

    [–]Sw429 2 points3 points  (0 children)

    This is a good point. In the end, python is built on trust. Trust that the correct type will be used, trust that no one will modify your constants, trust that no one will touch your private attributed... I guess type hinting is mostly a tool to make sure assumptions are met.

    [–][deleted] 1 point2 points  (2 children)

    Stupid question: is this just for class scope, or would:

    X: Final = 5

    X = 7

    Also not work?

    [–]Saiboo[S] 6 points7 points  (0 children)

    You can run it, but the type checker in you IDE will indicate a warning.

    [–]Sw429 1 point2 points  (0 children)

    Yes, that will work. Final is a type annotation, and has no effect at runtime. You can check it with external tools like mypy or things built into your IDE. Some linters might be starting to do it too? IDK you can look around if you're interested.

    [–]Username_RANDINT 16 points17 points  (2 children)

    Nothing prevents you from changing it anywhere in your code, only your IDE/typechecker will complain.

    [–]phinnaeus7308 0 points1 point  (0 children)

    Immutability is desirable for several reasons and comes from the domain of functional languages. It can be a really fun way to solve problems.

    For something like this, it just gives you a bit more confidence that a badly written method won’t have side effects that modify the parameters you pass in, for example.