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}

Why not just use that notation?

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.

Was it difficult?

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.

What’s Guido’s take on this (circa 2008)?

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.

What I learned: It’s not always hard to make interesting changes to large projects.

Check out the code

See Guido's Language Design Philosophy

Read the comments on HackerNews



