[Python-3000] sets in P3K?

Talin talin at acm.org
Thu Apr 27 04:03:02 CEST 2006


Greg Wilson <gvwilson <at> cs.utoronto.ca> writes:

> Hi Guido,
> 
> > On 4/24/06, Greg Wilson <gvwilson <at> cs.utoronto.ca> wrote:
> > > On a completely different note, are you willing to consider native
> > > syntax for sets (as per PEP 218) in Python 3000?
> 
> > I like to write {1, 2, 3} instead of set([1, 2, 3]) but I don't know
> > what to do with the ambiguity for {}. I believe you proposed {-} at the
> > time which is actually hard to parse (because the '-' looks like the
> > start of an expression and with our LL(1) parser there's no way back).
> > Ideally perhaps {:} would be an empty dict and {} an empty set -- but
> > that will take some getting used to (and would probably break
> > compatibility with JSON). Perhaps {/} for an empty set? I'd hate to have
> > to say set(). ABC solved this by having a special empty object that
> > could become either a list or a table, but I don't think that'll work
> > for us -- the implementation would have to have compatible memory
> > lay-outs.
> 
> I'm sure we can work something out --- I agree, {} for empty set and {:}
> for empty dict would be ideal, were it not for backward compatibility.  I
> liked the "special empty object" idea when I first wrote the PEP (i.e.,
> have {} be something that could turn into either a set or dict), but one
> of the instructors here convinced me that it would just lead to confusion
> in newcomers' minds (as well as being a pain to implement).

At the risk of creating further "ugliness", here is my humble proposal for a 
notation for set literals:

As suggested, the nominal notation is a comma-separated list, surrounded by 
braces:

   {1, 2, 3}

A set with a single element is:

   {1,}

In other words, we steal the idea from tuples - a trailing comma is used as a 
way to signal that this is a comma-separated list of one item.

A set with no elements is:

   set()

Alternatively, you could take the tuple idea one step further:

   {,}

However, I'm not sure I like this very much.

-- Talin




More information about the Python-3000 mailing list