What do you think about Python's new Set Literal (2.7a3+)?
>>> {1,1,2,3,3}
set([1, 2, 3])
Wouldn't it make some confusions with Array, If you came from different programming backgrounds like C/C++, C#, Java?
What do you think about Python's new Set Literal (2.7a3+)?
>>> {1,1,2,3,3}
set([1, 2, 3])
Wouldn't it make some confusions with Array, If you came from different programming backgrounds like C/C++, C#, Java?
I guess it will take some getting used to.
Remember though that math uses that notation for sets.
I think set comprehensions are going to be quite nice though
It's a pity that they didn't make nested sets work with that notation
>>> {1,2,{3,4,5}}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'set'
Perhaps f{} could make frozen sets the same say we use r'' for raw strings, then at least I could write
>>> {1,2,f{3,4,5}}
It makes sense, after all a set and a dictionary are related (I used to use a dictionary to emulate a set).
I'm not sure how often you need a set literal; if you're going to be listing the members literally, why not just avoid repeating them, and use a plain old list?
I also think it's weirdly inconsistent that you use {} to denote the set literal, but when printing itself, it uses []:
>>> {1,2,3,4,5}
set([1, 2, 3, 4, 5])
To me, it would make more sense if it repeated the actual literal syntax, i.e:
>>> {1,2,3,4,5}
{1, 2, 3, 4, 5}
or perhaps
>>> {1,2,3,4,5}
set({1, 2, 3, 4, 5})
Whatever your ground you find for it or against it, I feel "confusion with other language's features" is not a good justification.
It would also be consistent with all the collection literals you already have in Python.
[1, 2, 3]{"one":1, "two":2, "three":3}(1, 2, 3){1, 1, 2, 3}My bottom line is that improving intra-language consistency wins over avoiding inter-language confusion
If anything, confusion within the language should be taken into account. Now you have to remember that {} specifies an empty dictionary, not a set.