[Python-ideas] Immutable dictionaries

Rob Cliffe rob.cliffe at btinternet.com
Wed Nov 29 20:16:48 EST 2017



On 29/11/2017 17:30, Asen Bozhilov wrote:
> This is my first post here. I have strong experience with JavaScript 
> and I'm lucky that I could move forward to Python.
> What I miss in Python are immutable dictionaries. They are especially 
> useful for configurations and call functions which expect dictionary 
> as argument.  In my opinion they would let a place for underlying 
> optimizations.
>
> I'd like to propose also literaling syntax for immutable dictionaries.
>
> immutable_dict = (
>     'key1' : 'value1',
>     'key2' : 'value2'
> )
>
> This syntax is not ambiguous with expression statements and tuple 
> literals, but it requires a bit lookahed during the parsing.
>
>
This is sort of a subset of an idea I once posted on Python-Ideas: 
Dictionaries, sets and lists (etc. ?) could have a mutable flag, and 
once it was set to False you could neither change it back nor change the 
object.  (OK there might be some backdoor hacks, this is Python.)  This 
would avoid having to explain to beginners "Why does Python have lists 
and tuples?" because instead of a tuple, all you need is an immutable 
list.  (Or if you prefer, instead of a list, all you need is a mutable 
tuple.)
Rob Cliffe


More information about the Python-ideas mailing list