top | item 5384236

Adding Empty Set Literals to Python

41 points| philipbjorge | 13 years ago |philipbjorge.com | reply

46 comments

order
[+] rakha|13 years ago|reply
"I thought {@} would be a good for an empty set because it arguably looks like the empty set symbol"

I'm just nitpicking but I don't think this is a good justification - If the @ symbol looks like the empty set symbol, then {@} is the set containing the empty set

[+] jrockway|13 years ago|reply
I personally prefer dict() to {} anyway, so I don't really miss the lack of an empty set literal.

Adding a set literal was a slippery slope anyway. Where's my frozenset literal? Where's my namedtuple literal?

(I also don't particularly like the {} syntax for dicts, because there is {'too_much': 'quoting'} which I find rather ugly. I much prefer dict(too_much='quoting') or, even better, Perl's solution: { too_much => 'quoting' }.)

[+] mkhattab|13 years ago|reply
I also prefer the dict() function as well as JS object literals but it's too bad there's a performance penalty to using dict().
[+] raymondh|13 years ago|reply
Executive summary: If you don't like part of the language, it is really easy to add your own hack and make it worse ;-)

Really, you should trust the core developers on this one. Empty set literals are missing from Python for a reason.

[+] nnq|13 years ago|reply
You're right on trusting the core developers, but "for a reason" type arguments are not the way to do things. I love Python because it's so practical and manages to stop codebases from devolving into overengineered incomprehensible messes, but I hate tons lots of things about it, and more generally I hate its "worse is better" approach (http://www.jwz.org/doc/worse-is-better.html) to many things (because for Guido, if "worse" is "simpler to explain and implement", he always chooses it, always implementation simplicity over nice "interface" features and also over "interface" consistency... I almost want to shoot the man when I read arguments like http://www.artima.com/weblogs/viewpost.jsp?thread=147358 coming from a very smart person like him). OP's complaint is valid imho, consistency is f important, because small inconsistencies pile up and make a language harder to learn (yeah, you don't notice them once they're in your "muscle memory", but "learnability" is important for a language and this is why I like Python and I hope it doesn't lose this quality by slowly accreting inconsistencies). Hopefully his blogpost is a message that reaches Guido and the core developers and nudges them even a little bit towards a better path!
[+] philipbjorge|13 years ago|reply
I just wish someone could find the old discussions...

The main argument I could see against empty set literals was how wrong it felt adding it to the grammar. Even adding 3 lines of code for the feature, felt like it was just creating chaos in a zen-like codebase.

[+] polemic|13 years ago|reply
That ain't orthogonality, this is orthogonality:

    dict() = {}
    set() = <>
    tuple() = ()
    list() = []
(btw, does anyone actually use `<>` as `!=`?)
[+] derefr|13 years ago|reply
It seems to me that our real problem here is the dearth of easily-accessible bracket characters on US-standard keyboards:

    x = 【1, 2, 3】
    y = 【】
;)
[+] Erwin|13 years ago|reply
They finally got rid of <> in Python 3 (together with the `` as being alias for repr).
[+] hwiechers|13 years ago|reply
I think it would be best if empty dictionary was {:} and empty set was {}. Of course, it would be insane to make this change.
[+] sciencerobot|13 years ago|reply
Maybe Guido doesn't like it because it looks weird when compared to the rest of Python's syntax? Still, I think it's cool. Having sets availably without importing anything is one of the things I love about Python.

set = <> is still available.

[+] jmmcd|13 years ago|reply
set = <> is still available

Did this get misformatted? I don't know what you mean.

[+] uraza|13 years ago|reply
set = <> looks like the other empty container literals, but it's not consistent with the literal notation for sets with one or more elements: my_set = {1,2,3}.
[+] rdtsc|13 years ago|reply
you don't import sets , set() is available right away.
[+] codesuela|13 years ago|reply
The webfont doesn't load for me because I have Ghostery installed (it blocks remote tracking scripts) so the page is all blank except for the code snippets (because Adobe Typekit is blocked). You should consider adding a fallback font.
[+] philipbjorge|13 years ago|reply
Thanks and will do!

I just added a 1 second timeout that puts a default font on if the webfont hasn't loaded. Just tested with ghostery and it should be good to go.

[+] vbtemp|13 years ago|reply
> What I learned: It’s not always hard to make interesting changes to large projects.

Should be, it's not always hard to make interesting changes to very well-designed and well-architected projects

[+] johnsoft|13 years ago|reply
I vaguely recall seeing a mailing list thread where `{-}` was proposed. I think it was rejected based on parsing ambiguity, i.e. `{-1}` vs `{-}`. (Or it's possible I might be losing my memory)
[+] yen223|13 years ago|reply
Wouldn't it lead to the same problem of a lack of consistency?

    dict: {} 
    list: []
    tuple: ()
    set: {-}
[+] philipbjorge|13 years ago|reply
I originally implemented it that way and had that exact same issue (it's one of the revisions on github). Similarly, {/} would also be a problem.
[+] tantalor|13 years ago|reply

    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
[+] dechols|13 years ago|reply
Which is more Pythonic?

list() or []

dict() or {}

set() or ????

I would argue that the left is more pythonic and expressive.

[+] SilasX|13 years ago|reply
Is it just me, or is it a little ... obsessive to be this annoyed with having to use `set()` instead of `{@}`?
[+] npc|13 years ago|reply
What, you've never gone and hacked an interpreter because you were mildly annoyed by something? I don't think he actually means for it to be used in production.