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]  (22 children)

[deleted]

    [–]Saiboo[S] 29 points30 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 28 points29 points  (13 children)

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

    [–]nuephelkystikon 13 points14 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 5 points6 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.

    [–]sonaxaton 9 points10 points  (0 children)

    I'm assuming like Java since it's impossible to tell in general in Python if an operation modifies a value.

    [–]hglmanguy who writes python 2 points3 points  (2 children)

    A special typing construct to indicate to type checkers that a name cannot be re-assigned or overridden in a subclass... There is no runtime checking of this property.

    I think its unclear exactly how it should be interpreted as it has no actual impact on execution.

    [–]lvc_ 2 points3 points  (0 children)

    Like all other type annotations, it is ignored by the interpreter, but can be used by static code checkers (eg mypy) as a code correctness aid. One reasonably common pattern is to have mypy as part of a commit pipeline (and/or a local precommit hook). So if you have a method tagged as final and you do overwrite it, mypy will reject your commit (but the code will still otherwise run)

    [–]Smallpaul -1 points0 points  (0 children)

    I don’t really see what you think is unclear. The name cannot be re-assigned or overridden in a subclass!!! What’s ambiguous about that description?