[Python-ideas] Enhancing dict.values

Ethan Furman ethan at stoneleaf.us
Fri May 27 12:59:38 EDT 2016


On 05/27/2016 09:52 AM, M.-A. Lemburg wrote:
> On 27.05.2016 18:12, Ethan Furman wrote:
>> On 05/27/2016 08:02 AM, Michael Selik wrote:
>>> On Thu, May 26, 2016 at 11:39 PM Steven D'Aprano wrote:
>>>> On Thu, May 26, 2016 at 11:28:25PM +0100, Nathan Schneider wrote:
>>
>>>>> Instead of special syntax, what if dict.values() returned a tuple
>>>>> when given keys as arguments:
>>>>>
>>>>>     partner_id, product_id, ship_to, product_ids = my_dict.values(
>>>>>             'partner_id', 'product_id', 'ship_to', 'product_ids')
>>>>
>>>> I like this idea. I think it beats the status quo:
>>>
>>> Isn't this the status quo?
>>>       a, b, c = [mapping[k] for k in ('a', 'b', 'c')]
>>
>> Yes.
>
> Uhm, what about...
>
> from operator import itemgetter
>
> a, b, c = itemgetter('a', 'b', 'c')(mapping)

I may have misspoken.  The probably most used status quo would be:

   partner_id = values['partner_id']
   state = values['state']
   due_date = values['due_date']
   ...

So far, the only syntax I've seen that is both readable and has even a 
remote shot at acceptance would be:

   {partner_id, state, due_date, **rest} = values

The consensus is that that is too magical (which I can't argue with ;).

itemgetter, comprehensions, name changes, etc., are all (extremely) verbose.

--
~Ethan~


More information about the Python-ideas mailing list