[Python-Dev] An unambiguous way of initializing an empty dictionary and set

33 views
Skip to first unread message

joao.p.f....@gmail.com

unread,
Mar 13, 2022, 5:55:16 PM3/13/22
to pytho...@python.org
Currently:
l = [] # new empty list
t = () # new empty tuple
s = set() # new empty set (no clean and consistent way of initializing regarding the others) <<<
d = {} # new empty dictionary

Possible solution:
s = {} # new empty set
d = {:} # new empty dictionary (the ":" is a reference to key-value pairs)

Current workaround at least for consistency:
l = list() # new empty list
t = tuple() # new empty tuple
s = set() # new empty set
d = dict() # new empty dictionary

However, it doesn't feel right to not be able to initialize an empty set as cleanly and consistently as lists, tuples and dictionaries in both forms.
_______________________________________________
Python-Dev mailing list -- pytho...@python.org
To unsubscribe send an email to python-d...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/pytho...@python.org/message/3VPVY2BUHAULUBBQD3UKUIOPQXUZOLLP/
Code of Conduct: http://python.org/psf/codeofconduct/

Ethan Furman

unread,
Mar 13, 2022, 6:06:29 PM3/13/22
to python...@python.org, pytho...@python.org
On 3/13/22 14:49, joao.p.f....@gmail.com wrote:

> Currently:
> l = [] # new empty list
> t = () # new empty tuple
> s = set() # new empty set (no clean and consistent way of initializing regarding the others) <<<
> d = {} # new empty dictionary
>
> Possible solution:
> s = {} # new empty set
> d = {:} # new empty dictionary (the ":" is a reference to key-value pairs)
>
> Current workaround at least for consistency:
> l = list() # new empty list
> t = tuple() # new empty tuple
> s = set() # new empty set
> d = dict() # new empty dictionary
>
> However, it doesn't feel right to not be able to initialize an empty set as cleanly and
> consistently as lists, tuples and dictionaries in both forms.

This is a topic better fit to python-ideas.

--
~Ethan~
_______________________________________________
Python-Dev mailing list -- pytho...@python.org
To unsubscribe send an email to python-d...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/pytho...@python.org/message/AHUHYKKSVXSAIVYVAMNZDLFJQI4BEJU5/

joao.p.f....@gmail.com

unread,
Mar 13, 2022, 6:12:24 PM3/13/22
to pytho...@python.org
Thanks!
_______________________________________________
Python-Dev mailing list -- pytho...@python.org
To unsubscribe send an email to python-d...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/pytho...@python.org/message/KYPH76YPDDZFZRM2XD3YWNSN2YZUYLIY/

Christopher Barker

unread,
Mar 13, 2022, 6:49:32 PM3/13/22
to joao.p.f....@gmail.com, pytho...@python.org
Possible solution:
s = {} # new empty set
d = {:} # new empty dictionary (the ":" is a reference to key-value pairs)

This would be fine for a new language. But completely out of the question for Python — it would break an enormous amount of code.

We are in this position because sets are relatively new to Python, and there are only so many brackets.

Current workaround at least for consistency:
l = list() # new empty list
t = tuple() # new empty tuple
s = set() # new empty set
d = dict() # new empty dictionary

However, it doesn't feel right to not be able to initialize an empty set as cleanly and consistently as lists, tuples and dictionaries in both forms.

It may not “feel” right, but is it that big a deal? There are literally any number of types that can’t be initialized with a “literal” — so consistence is not compelling here. set() is (maybe?) the only builtin, but is initializing and empty set that common? 

Note, there was a recent thread on this list about a literal for frozenset — I think:

f{} was proposed— you may want to revive that -and add s{} for an empty set …

Though i personally wouldn’t support the idea.

-CHB



--
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython

Christopher Barker

unread,
Mar 13, 2022, 6:51:50 PM3/13/22
to joao.p.f....@gmail.com, pytho...@python.org

Oops, didn’t notice this wasn’t Python-ideas — that’s where it should be.

-CHB

Terry Reedy

unread,
Mar 13, 2022, 6:55:39 PM3/13/22
to pytho...@python.org
On 3/13/2022 5:49 PM, joao.p.f....@gmail.com wrote:
> Currently:
> l = [] # new empty list
> t = () # new empty tuple
> s = set() # new empty set (no clean and consistent way of initializing regarding the others) <<<
> d = {} # new empty dictionary
>
> Possible solution:
> s = {} # new empty set
> d = {:} # new empty dictionary (the ":" is a reference to key-value pairs)

This is such a good idea that many people, including myself, have
already had it, and even more agree that this would be the proper way
for a new language. But as already discussed on python-ideas, this
change would break possibly millions of programs, and we will will not
do that. Please don't futilely push this further.

> Current workaround at least for consistency:
> l = list() # new empty list
> t = tuple() # new empty tuple
> s = set() # new empty set
> d = dict() # new empty dictionary

Anyone who values consistency can pay the price of this in ones own code.

If you have questions, python-list would be the appropriate place.

--
Terry Jan Reedy

_______________________________________________
Python-Dev mailing list -- pytho...@python.org
To unsubscribe send an email to python-d...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/pytho...@python.org/message/5XVT47A2AWGKM3BRJD27OD3I64IPEQ2N/

Jeremiah Vivian

unread,
Mar 13, 2022, 8:15:31 PM3/13/22
to pytho...@python.org
> s = {} # new empty set
> d = {:} # new empty dictionary (the ":" is a reference to key-value pairs)
Even though this isn't python-ideas, I just want to suggest having it like this:
s = {,} # new empty set
d = {} # same syntax for new empty dictionary
Instead of the backwards-incompatible change above.
_______________________________________________
Python-Dev mailing list -- pytho...@python.org
To unsubscribe send an email to python-d...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/pytho...@python.org/message/KHN7GOKSUXA22ZZK7Y67UPM6QYOVDLAQ/
Reply all
Reply to author
Forward
0 new messages