[Python-ideas] Possible method of distinguishing between set-literals, dict-literals, and odict-literals
Guido van Rossum
guido at python.org
Tue Jun 16 20:36:44 CEST 2009
On Tue, Jun 16, 2009 at 11:24 AM, Terry Reedy<tjreedy at udel.edu> wrote:
> Carl Johnson wrote:
>>
>> I thought of this in the shower. It might not be a good idea, but I'd
>> like to hear what other people think. On analogy with b"", r"", etc.
>> we could introduce an empty set-literal and an odict-literal, and add
>> a more explicit form to replace the existing set literal. s{} could be
>> the empty set, o{} could be an empty odict, and we could leave {}
>> alone as the form for dicts. So, an odict literal would look like
>> o{'a':'1', 'b':'2', 'c':'3'} instead of OrderedDict([('a', '1'), ('b',
>> '2'), ('c', '3')]). And the set {'a', 'c', 'b'} could (optionally?)
>> have a little s{'a', 'c', 'b'} to make it more explicit that this is a
>> set, not a dict.
>>
>> I suppose if we wanted to get crazy, we could also have a frozenset
>> literal, fs{}…
>>
>> So what do people think? Is this too ugly to do? Does it confuse users
>> who are used to C-style braces? Or is it a logical extension of the
>> b"", r"", etc. system that could help make things follow EIBI better?
-1 from me.
> I consider b"" and r"" to be hacks, necessary but not to be imitated without
> such necessity. I believe both affect how the lexer interprets code
> characters when it constructs literal value tokens to be fed to the parser.
Not quite -- the lexer passes the entire string literal uninterpreted
to the parser (including quotes). It doesn't even need the 'b' or 'r'
prefix to tell where the literal ends (though it does need the opening
quote for that, and whether it's tripled). The code generation stage,
however, looks at the prefix and interprets the contents of the
literal differently based on the presence of 'b', and creates a
different object depending on the presence of 'b'. (Or 'u' in Python
2.)
> Collection-content displays are not, in general, constant and generally
> produce run-time code.
(?)
> OrderedDict({'a':1', 'b':'2', 'c':'3'}]
>
>
>> How about ['a':'1', 'b':'2', 'c':'3']?
-100.
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-ideas
mailing list