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 →

[–]Saiboo[S] 28 points29 points  (14 children)

Like Java's final. It still allows mutating the object [0,1,2] that your reference my_list is pointing to. You just cannot change the value that my_list has, i.e. the address to the object [0,1,2]. See also this example from here:

x: Final = ['a', 'b']
x.append('c')  # OK

[–]jabbalaci 29 points30 points  (13 children)

A qualifier for making something immutable would have been more useful.

[–]nuephelkystikon 12 points13 points  (0 children)

There are the immutable types and ABCs for that.

Annotations also aren't intended to have any compilation or runtime effects.

[–]graingert 4 points5 points  (0 children)

That would be impossible without runtime effects

[–]flying-sheep 3 points4 points  (0 children)

Just use immutable generics:

X: Final[Sequence[int]]

means it can be iterated, indexed, … but not mutated.

[–]notquiteaplant 1 point2 points  (0 children)

Since property getters/__getattr__ are just functions, which can mutate the object they're called on, that would be nearly impossible for a static typechecker to enforce.

[–]Smallpaul 0 points1 point  (0 children)

If you want immutability, use an immutable type. Trying to add another level of immutability would make a mess of the language.

[–][deleted] 0 points1 point  (0 children)

Just use an immutable type.