Python’s lack of empty set literals bothers me. In a language known for its orthogonality, it’s an odd omission. The chart just feels unbalanced to me.
dict() = {}
set()
tuple() = ()
list() = []
In Python 2.7+, there exists a literal notation for sets with one or more elements.
one_element_set = {1}
two_element_set = {1, 2}
You probably already see why adding empty set notation is a pain – both an empty dictionary and an empty set share the same natural notation ({}). I thought {@} would be a good for an empty set because it arguably looks like the empty set symbol and doesn’t cause any problems with the grammar.
With a little help from Eli Bendersky‘s blog post, I knew roughly where to start surgery in Python’s source code (ack was my friend). After discovering where to add the {@} construct to the grammar and how the abstract syntax tree was structured using gdb I managed to add the feature with only 3 lines of code. Way cool!
The if statement in Python/ast.c is checking for the following structure in the AST.
Right. All possible proposals have already been discussed at length.
Really, writing set() isn’t so bad.
Get used to it.
I couldn’t find any of the preceding discussions on google, so if anyone could link me to them that’d be awesome. That being said, I knew the feature was not being considered before I started and was interested in learning a little more about Python’s internals which were surprisingly understandable.
Check out the code
See Guido's Language Design Philosophy
Read the comments on HackerNews
Matt Joiner
March 16, 2013 at 8:18 am
You’ve tagged your post C++, but CPython is most definitely not implemented in C++.
philipbjorge
March 16, 2013 at 5:37 pm
Thanks for noticing this. I just went to fix it and apparently wordpress is having issues because the slug for the c and c++ tags are the same, so it might be a bit before I can change this (and fix old posts).