[Python-Dev] syntactic support for sets (original) (raw)

Donovan Baarda abo at minkirri.apana.org.au
Fri Feb 3 13:04:52 CET 2006


On Wed, 2006-02-01 at 13:55 -0500, Greg Wilson wrote:

Hi,

I have a student who may be interested in adding syntactic support for sets to Python, so that: x = {1, 2, 3, 4, 5} and: y = {z for z in x if (z % 2)}

Personally I'd like this. currently the "set(...)" syntax makes sets feel tacked on compared to tuples, lists, dicts, and strings which have nice built in syntax support. Many people don't realise they are there because of this.

Before set() the standard way to do them was to use dicts with None Values... to me the "{1,2,3}" syntax would have been a logical extension of the "a set is a dict with no values, only keys" mindset. I don't know why it wasn't done this way in the first place, though I missed the arguments where it was rejected.

As for frozenset vs set, I would be inclined to make them normal mutable sets. This is in line with the "dict without values" idea.

Frozensets are to sets what tuples are to lists. It would be nice if there was another type of bracket that could be used for frozenset... something like ':1,2,3:'... yuk... I dunno.

Alternatively you could to the same thing we do with strings; add a prefix char for different variants; {1,2,3} is a set, f{1,2,3} is a frozen set...

For Python 3000 you could extend this approach to lists and dicts; [1,2,3] is a list, f[1,2,3] is a "frozen list" or tuple, {1:'a',2:'b'} is a dict, f{1:'a',2:'b'} is a "frozen dict" which can be used as a key in other dicts... etc.

-- Donovan Baarda <abo at minkirri.apana.org.au> http://minkirri.apana.org.au/~abo/



More information about the Python-Dev mailing list