tags:

views:

360

answers:

5

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?

+3  A: 

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}}
gnibbler
I didn't know about set comprehensions until just now. Cool! From http://docs.python.org/3.1/tutorial/datastructures.html :Like for lists, there is a set comprehension syntax:`>>> a = {x for x in 'abracadabra' if x not in 'abc'}`_`>>> a`_`{'r', 'd'}`
MatrixFrog
Thanks, I didn't know that math using that for sets, good then.
S.Mark
+3  A: 

It makes sense, after all a set and a dictionary are related (I used to use a dictionary to emulate a set).

Stefano Borini
+1  A: 

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})
unwind
I haven't read the PEP, but I think it would make sense for the behaviour of printing would change eventually. Perhaps they left it that way to not break too many doctests?
gnibbler
good point, also `{[1,2,3,4,5]}` or `{*[1,2,3,4,5]}` getting syntax error currently.
S.Mark
`>> To me, it would make more sense if it repeated the actual literal syntax`.it's time for a change: Python 3.x already does this.
Adrien Plisson
`"why not just avoid repeating them, and use a plain old list?"` Because this doesn't guarantee set-like behavior for adding new elements later on.
Rafał Dowgird
@Rafał Dowgird: No, but since you already could initialize the set from a list, you would get a set, but using an existing literal in the constructor. I guess the new literal cuts down on typing, of course.
unwind
@S.Mark, `{[1,2,3,4,5]}` should be a TypeError, not a SyntaxError. `{*[1,2,3,4,5]}` should be a SyntaxError; it's not a function call and there's no reason it should support the `*` unpacking syntax. `{*q}` is spelled `set(q)`.
Mike Graham
@gnibbler, Anyone who depends on the repr of a set in their doctest is already in deep trouble.
Mike Graham
Thanks @Mike Graham, you're right, and thats make sense.
S.Mark
+18  A: 

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.

  • List: [1, 2, 3]
  • Dictionary: {"one":1, "two":2, "three":3}
  • Tuple: (1, 2, 3)
  • And why not set? {1, 1, 2, 3}

My bottom line is that improving intra-language consistency wins over avoiding inter-language confusion

flybywire
Hear, hear. Much truth in this post.
mizipzor
+1: Confusion with other languages could be the worst possible reason. It leads to all languages having the same syntax.
S.Lott
You said the truth, but just saying **"why not set?"** didn't answer my question, so I didn't accept this.
S.Mark
+2  A: 

If anything, confusion within the language should be taken into account. Now you have to remember that {} specifies an empty dictionary, not a set.

Rafał Dowgird
+1 good point, `{}` is dict, `{1}` is set
S.Mark
Yup, that's what I had in mind. I can imagine myself mistakenly changing the former to the latter ("Oh, a quick fix, I'll just change the init set to an empty one..." whoops!)
Rafał Dowgird