[Python-ideas] Unpacking a dict

Ethan Furman ethan at stoneleaf.us
Wed May 25 19:25:47 EDT 2016

On 05/25/2016 04:03 PM, Sven R. Kunze wrote:
> 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

No.  I'm saying attribute access is already extremely easy, so we don't 
need to try and make it easier.

> 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.

And this newbie would be wrong, as [other stuff] for a mapping can 
easily be integers just like lists/tuples.

> 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.

We already have that situation:

   some_var = [1, 2]
   a = some_var[0]
   b = some_var[1]

is exactly the same as

   a, b = some_var

and the visual indicators (the integers, the item access) are nowhere to 
be seen.


More information about the Python-ideas mailing list