Yeah all good points, I think my final thoughts on this would be.
There should be a version of python that introduces the “.frozen/.freeze” property method approach discussed above similar to how async was introduced as plain function calls before becoming core syntax. Better to have something in a clunky manner than not at all.
If this becomes syntax, I think the way to solve this is not introducing a bunch of new literals you have to remember I actually think the fix is to introduce constants to python as a whole (Which don’t currently exist). This could be a soft keyword at assignment similar to how global works today i.e const NAMES = ["James", "Bob"] so it does not conflict with any existing var names and it allows this to be extended so runtime can enforce variable should not change, can optimise away making a frozen list AND potentially allow objects to participate in ensure they become immutable.
Note frozen and const are not necessarily synonymous (i.e., if tuple is considered frozen and const means it cannot change). See this snippet:
t = (1, [], 3.14) # 3-element tuple with `t[1]` an empty `list`
t[1].append("test")
t[1].append(2) # now the `list` is no longer empty
t
# (1, [‘test’, 2], 3.14) # different tuple than before, so not `const`
That is true of a language that does not treat array, sets or dicts as being a value type during const expressions such as C etc.
But languages like swift where they are treated as a value type then they are the same, python could go down the same route and guarantee all values in a collection marked as const are also const.
During the AST phase it could identify that an assignment expression has been marked const and swap for the frozen in the same manner we are proposing for .frozen