[Python-ideas] Unpacking a dict

Sven R. Kunze srkunze at mail.de
Wed May 25 19:03:40 EDT 2016


On 25.05.2016 22:11, Ethan Furman wrote:
> On 05/25/2016 12:38 PM, Sven R. Kunze wrote:
>> Maybe, it's just me but I still tend to think that using unquoted
>> strings should be reserved for attribute unpacking.
>
> lists, tuples, and dicts are basic containers -- having syntax to 
> unpacak them easily is a clear win;  attribute unpacking not so much:

Wait a second. Reading your example below, do you imply that dicts 
actually should provide something like a attribute access to its keys?

 >>> d = {'a': 1,'b': 2}
 >>> d.b
2

Wouldn't this solution be equally readable/useful? I am just asking as 
it seems that you find accessing a dict to be straining. So, unpacking 
it would a one solution but providing attribute-like access to it would 
be another solution to that problem.

> --> some_long_object_name = MyClass(blah, blah)
> --> s = some_long_object_name
> --> s.name
> some value
> --> s.value
> --> another value
>
> which is sufficiently readable.

I see your point.

But let me explain how I approached the problem by considering it from 
the the "new users" perspective. Really fast, he will build up the 
following associations:

attributes <-> .abc
dict keys <-> ['abc'] or [other stuff]
list/tuple keys <-> [123]

So, from his perspective everything is clearly separated: lists/tuples 
use integers, dicts use mostly strings with quotes and objects have 
attributes which can be used like normal variables without quotes.

Mixing things up (by removing/mixing the visual indicators), makes me 
feel nervous about the consistent perception of Python. Maybe, it's just 
FUD on my side but I can't help this feeling.


Best,
Sven


More information about the Python-ideas mailing list