[Python-ideas] Unpacking a dict
Ethan Furman
ethan at stoneleaf.us
Wed May 25 23:46:55 EDT 2016
On 05/25/2016 07:56 PM, Chris Angelico wrote:
> On Thu, May 26, 2016 at 12:43 PM, Ethan Furman wrote:
>> I'll meet you halfway:
>>
>> {a, b} = some_dict
>>
>> which is currently a SyntaxError, so little backwards compatibility
>> concerns, plus it clearly state the mapping should have two elements,
>> similarly to
>>
>> [a] = some_iterable
>>
>> and
>>
>> (b) = some_iterable
>>
>> both clearly state that a one-element iterable is being unpacked.
>>
>
> Careful - the second one doesn't:
>
>--> (b) = [1, 2, 3]
>-->
Ack. Right you are, which is why I always use the list form, even on
tuples.
> I hope there doesn't end up being a confusion between mapping
> unpacking and set display. Sets are a bit of an odd duck; are they
> like lists only unordered, or like mappings only without values? I've
> seen them used both ways, and the syntax is somewhere between the two.
> Having a syntax that constructs a set if used on the RHS but unpacks a
> dict if used on the LHS seems to violate syntactic purity, but I'd be
> happy to let practicality trump that.
Given the other issues, I'm happy to let this one die. Good discussion.
--
~Ethan~
More information about the Python-ideas
mailing list