Adding Empty Set Literals to Python
philipbjorge.com
philipbjorge.com
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
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' }.)
I agree, but if it wasn't for the fact that using dict() is six times slower than using literals then it wouldn't make much of a difference arguing code style [1].
> (By that logic, you should represent sets as {'element': 1} instead of set('element'), because calling the function "set" is slower than making a dictionary object from the special syntax.)
Which is why I guess there are now set literals in Python (just not for empty sets :).
-- [1]: http://doughellmann.com/2012/11/the-performance-impact-of-us...
I think the reason for having set literals is not performance but completeness and prettier syntax.
Really, you should trust the core developers on this one. Empty set literals are missing from Python for a reason.
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.
dict() = {}
set() = <>
tuple() = ()
list() = []
(btw, does anyone actually use `<>` as `!=`?)Of course `<1, 2, 3, 4>` would then have to be an alternative set literal.
<pedant type="confused">emphatic?</pedant>Not any more than `[` being used for both literal lists and indexing.
> If it wouldn't cause trouble in the formal grammar, then at least in terms of readability.
It wouldn't cause the slightest trouble in the grammar, and I don't think it'd cause much if any trouble for readability either.
Also, having braces for sets happens to coincide with mathematical notation. Angular brackets would be more appropriate for tuples, in this regard--although the brackets used for tuples in mathematical notation are less acute: \langle and \rangle in LaTeX.
I think it's pretty unfortunate that for practical purposes we're forced to make do with what's available in the ASCII character set, which is pretty much an accident of history. My favorite solution to the OP's problem would be to use the Unicode empty set symbol. People who for some reason aren't able to type it could fall back to set(), and in terms of reading it will be immediately obvious to everyone that the empty set is intended, which doesn't hold for all the ad-hoc solutions suggested until now.
x = 【1, 2, 3】
y = 【】
;)set = <> is still available.
Did this get misformatted? I don't know what you mean.
I think that's better than using the same brace-type for sets and dicts anyway, and it can't be as confusing as () == tuple(), (x) == x, (x,) == tuple([x]).
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.
Should be, it's not always hard to make interesting changes to very well-designed and well-architected projects
dict: {}
list: []
tuple: ()
set: {-} 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.list() or []
dict() or {}
set() or ????
I would argue that the left is more pythonic and expressive.
I don't think any are more pythonic. They are all valid python.