[Python-ideas] syntax for set

Ron Adam rrr at ronadam.com
Tue Nov 16 00:27:08 CET 2010



On 11/15/2010 02:56 PM, Greg Ewing wrote:
> Guido van Rossum wrote:
>> There is nothing obvious about {:} for the empty dictionary. :-)
>
> I think it has about the same level of obviousness as (x,)
> for a 1-element tuple. You would have to learn it, but
> after that you could easily remember which is which, because
> dicts are made with colons and sets aren't.
>
> A backwards-compatible alternative would be to use {.}
> for an empty set. Think of a set as half a dict, and the
> dot as half a colon. :-)

"set()" is only two chars longer than any of the suggestions. It's also 
clear about what it is.  The spelling isn't the issue.

In fact, if we can optimise some common constructors, such as in the cases 
of an empty containers, we can toss out the special syntax's.


ra:~$ python3.2 -m timeit "dict()"
1000000 loops, best of 3: 0.401 usec per loop
ra:~$ python3.2 -m timeit "{}"
10000000 loops, best of 3: 0.0649 usec per loop

ra:~$ python3.2 -m timeit "list()"
1000000 loops, best of 3: 0.284 usec per loop
ra:~$ python3.2 -m timeit "[]"
10000000 loops, best of 3: 0.0724 usec per loop

ra:~$ python3.2 -m timeit "tuple()"
1000000 loops, best of 3: 0.222 usec per loop
ra:~$ python3.2 -m timeit "()"
10000000 loops, best of 3: 0.0456 usec per loop

ra at Gutsy:~$ python3.2 -m timeit "set()"
1000000 loops, best of 3: 0.234 usec per loop


Not seriously*, ;-)
    Ron


* IMO: The real value of the special syntax is that it makes it easy to 
write out nested objects in a concise way.  The missing empty set syntax 
doesn't cause much problems with that.

  line_data = {'points':[(1 , 2), (3, 4)], 'color':'blue', 'style':'dotted'}





More information about the Python-ideas mailing list