[Python-3000] Set literal

Nick Coghlan ncoghlan at gmail.com
Fri Jan 25 17:05:15 CET 2008


Mark Summerfield wrote:
> On 2008-01-25, Guido van Rossum wrote:
>> For the record, I'm thinking Raymond has won this argument fair and
>> square, and I'm withdrawing my opposition.
>>
>> I hope it isn't too confusing that {1: 1} creates a *mutable* dict
>> while {1} creates an *immutable* frozenset. I still find this slightly
>> inelegant. But the practicality of being able to treat set literals as
>> compile-time constants wins me over.
> 
> So this will produce:
> 
>     frozenset()	# empty frozen set
>     {1}	    	# 1 item frozen set
>     {1, 2}  	# 2 item frozen set
>     {}	    	# empty dict
>     {1:1}   	# 1 item dict
>     {1:1, 2:2}	# 2 item dict

More completely:

      ()           # empty tuple
      (1,)         # 1 item tuple
      (1, 2)       # 2 item tuple
      []           # empty list
      [1]          # 1 item list
      [1, 2]       # 2 item list
      {}           # empty dict
      {1:1}        # 1 item dict
      {1:1, 2:2}	  # 2 item dict
      frozenset()  # empty frozen set
      {1}          # 1 item frozen set
      {1, 2}       # 2 item frozen set
      set()        # empty mutable set
      set({1})     # 1 item mutable set
      set({1, 2})  # 2 item mutable set

So with Raymond's proposal we will have syntax for two immutable 
literals (tuples, frozensets) and two mutable container displays (lists, 
dicts).

Yes, there will be a few anomalies to learn in this list:
   - 1-tuples require a trailing comma
   - {} is a dict rather than a frozen set
   - frozen sets are immutable while dicts are mutable

Do these anomalies make this area of the language syntax harder to 
learn? Almost certainly - the 1-tuple anomaly has been tripping people 
up for years. Despite any reservation, are there valid reasons for 
having these anomalies in Py3k? As far as I am concerned, yes there 
are*, and I believe that is Guido's view as well.

Cheers,
Nick.

*Taking them from the top:
- 1-tuples require a trailing comma to differentiate them from the use 
of parentheses for mere expression grouping. Expression grouping is kind 
of important, and this anomaly in the syntax is a small price to pay for 
making that work intuitively.
- {} is used extensively in existing code (both operational code and 
code in documentation and other examples). Py3k may lower the bar for 
'acceptable breakage' in the realm of backwards compatibility, but it 
doesn't get rid of it altogether - and changing the meaning of {} fails 
to clear even that lowered hurdle. Also, as Marcin pointed out, an empty 
frozenset() is pretty useless, while an empty dict() is common.
- making set literals immutable provides excellent optimisation 
opportunities, which is important because it is a concern for speed 
which is likely to lead to the use of a set in the first place. It is 
also convenient in that set() is a lot easier to type than frozenset(), 
so going from an immutable literal to a mutable container is easier than 
going the other way would have been.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list