Set literals

G

George Sakkis

How about overloading curly braces for set literals, as in

- It is the standard mathematic set notation.
- There is no ambiguity or backwards compatibility problem.
- Sets and dicts are in many respects similar data structures, so why not share the same delimiter ?

*ducks*
 
G

George Sakkis

- There is no ambiguity or backwards compatibility problem.

....at least if it wasn't for the empty set.. hmm...
 
S

simon

+1 from me.

The other possible meaning for {1,2,3} would be {1:None,2:None,3:None},
but that is usually meant to be a set anyway (done with a dict).

So what is this: {1:2, 3, 4 } (apart from "nearly useless") ?

hmmm, thinking a bit more about this, it seems
you can build a set from a dict's keys, but not the other
way around. Is this odd, or what ?
a = set({1:0,2:0,3:0})
a set([1, 2, 3])

dict(a)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: cannot convert dictionary update sequence element #0 to a
sequence

Simon.
 
G

George Sakkis

+1 from me.

The other possible meaning for {1,2,3} would be {1:None,2:None,3:None},
but that is usually meant to be a set anyway (done with a dict).

So what is this: {1:2, 3, 4 } (apart from "nearly useless") ?

Syntax error; you'll have to decide whether you want a set or a dict.
hmmm, thinking a bit more about this, it seems
you can build a set from a dict's keys, but not the other
way around. Is this odd, or what ?
a = set({1:0,2:0,3:0})
a set([1, 2, 3])

dict(a)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: cannot convert dictionary update sequence element #0 to a
sequence

Simon.

Nothing odd here. The set constructor takes an iterable, and dict.__iter__ iterates through the
dict's keys, so the set a in your example doesn't know anything about the dict's values. You can go
back and forth a set and a dict if you store the dict's items instead:
a = set({1:0,2:0,3:0}.iteritems())
a set([(1,0), (2,0), (3,0)])
dict(a)
{1:0, 2:0, 3:0}

Regards,
George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top