[Python-ideas] Enhancing dict.values

Paul Moore p.f.moore at gmail.com
Fri May 27 14:28:15 EDT 2016


On 27 May 2016 at 18:42, Steven D'Aprano <steve at pearwood.info> wrote:
> On Fri, May 27, 2016 at 05:42:44PM +0100, Paul Moore wrote:
>
>> > - Any other functionality?
>>
>> Nested unpacking:
>>
>> location = = { 'name': 'The north pole', 'position': { 'x': 0, 'y': 0 }}
>> location_name, x, y = location.getvalues('name', 'position.x', 'position.y')
>
> How do you distinguish between a key 'position.x' and a key 'position'
> with a dict with a key 'x'?
>
> Nested unpacking seems too Javascripty for my liking.

Good point.

The problem is that *without* handling nested unpacking (and in
particular, the error checking needed - what if location['position']
isn't a dict? I'd like a uniform error I can trap or report on) I
don't see any practical advantage over

location_name = location['name']
x = location['position']['x']
y = location['position']['y']

So let's turn the question round. What advantages does the getvalues()
proposal have over the above sequence of assignments? I'm genuinely no
longer sure.

Paul


More information about the Python-ideas mailing list