[Python-ideas] namedtuple literals [Was: RE a new namedtuple]
Chris Angelico
rosuav at gmail.com
Thu Jul 27 17:50:52 EDT 2017
On Fri, Jul 28, 2017 at 7:22 AM, Pavol Lisy <pavol.lisy at gmail.com> wrote:
> On 7/26/17, Steven D'Aprano <steve at pearwood.info> wrote:
>
> [...]
>
>> But this has a hidden landmine. If *any* module happens to use ntuple
>> with the same field names as you, but in a different order, you will
>> have mysterious bugs:
>>
>> x, y, z = spam
>>
>> You expect x=2, y=1, z=0 because that's the way you defined the field
>> order, but unknown to you some other module got in first and defined it
>> as [z, y, x] and so your code will silently do the wrong thing.
>
> We have:
> from module import x, y, z # where order is not important
>
> Could we have something similar with ntuple (SimpleNamespace, ...)?
>
> maybe:
> for x, y, z from spam:
> print(x, y, z)
What you're asking for is something like JavaScript's "object
destructuring" syntax. It would sometimes be cool, but I haven't ever
really yearned for it in Python. But you'd need to decide whether you
want attributes (spam.x, spam.y) or items (spam["x"], spam["y"]). Both
would be useful at different times.
ChrisA
More information about the Python-ideas
mailing list