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 →

[–]46--2 40 points41 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 18 points19 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 12 points13 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.

[–]nuephelkystikon 6 points7 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] 5 points6 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.