Re: [Python-ideas] Immutable dictionaries
2017-11-30 2:16 GMT+01:00 Rob Cliffe : > 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.) > Here is a concept implementation of a freeze() function which does something similar. Rather than mutating the mutable object to become immutable, it makes an immutable copy. >>> freeze({"x": [1,2,{3,4}]}) mappingproxy({'x': (1, 2, frozenset({3, 4}))}) https://gist.github.com/stephanh42/d277170dd8a3a2f026c272a4fda15396 Stephan > Rob Cliffe > > ___ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ > ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Immutable dictionaries
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 ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Immutable dictionaries
David Mertz wrote: > https://www.python.org/dev/peps/pep-0416/ PEP 351 (also rejected) is related to this. -Barry ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Immutable dictionaries
2017-11-29 18:30 GMT+01:00 Asen Bozhilov : > I'd like to propose also literaling syntax for immutable dictionaries. > > immutable_dict = ( > 'key1' : 'value1', > 'key2' : 'value2' > ) Since Python 3.3, you can write: vstinner@apu$ python3 Python 3.6.3 (default, Oct 9 2017, 12:07:10) >>> import types >>> immutable_dict = types.MappingProxyType({"key": "value"}) >>> immutable_dict.pop('key') AttributeError: 'mappingproxy' object has no attribute 'pop' >>> immutable_dict['key'] = 'value2' TypeError: 'mappingproxy' object does not support item assignment >>> immutable_dict['key2'] = 'value3' TypeError: 'mappingproxy' object does not support item assignment Maybe not the ideal syntax, but it already works without having to modify the Python syntax, and it works on Python 3.3 and newer ;-) Victor ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Immutable dictionaries
On Wed, Nov 29, 2017, at 12:30, Asen Bozhilov wrote: > I would appreciate your opinions on the topic. Most interesting for me is > why they are not already part of the language? See the rejection notes at https://www.python.org/dev/peps/pep-0416/ ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Immutable dictionaries
https://www.python.org/dev/peps/pep-0416/ Also: https://pypi.python.org/pypi/frozendict On Wed, Nov 29, 2017 at 9:30 AM, 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. > > I would appreciate your opinions on the topic. Most interesting for me is > why they are not already part of the language? > > Kind regards, > Asen Bozhilov > > ___ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ > > -- Keeping medicines from the bloodstreams of the sick; food from the bellies of the hungry; books from the hands of the uneducated; technology from the underdeveloped; and putting advocates of freedom in prisons. Intellectual property is to the 21st century what the slave trade was to the 16th. ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Immutable dictionaries
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. I would appreciate your opinions on the topic. Most interesting for me is why they are not already part of the language? Kind regards, Asen Bozhilov ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/