[Python-ideas] namedtuple literals [Was: RE a new namedtuple]
Steven D'Aprano
steve at pearwood.info
Mon Jul 31 00:34:04 EDT 2017
On Sun, Jul 30, 2017 at 09:57:19PM +0300, Markus Meskanen wrote:
> So yeah, the order of the keyword arguments would matter in my case, and
> I've found it to work the best. How often do you get the keywords in a
> random order?
"Random" order? Never.
*Arbitrary* order? All the time. That's the whole point of keyword
arguments: you don't have to care about the order.
> And for those cases, you have SimpleNameSpace,
Which is no good for when you need a tuple.
> or you can
> just use the old namedtuple. But most of the time you always have the same
> attributes in the same order (think of reading a CSV for example),
If you're reading from CSV, you probably aren't specifying the arguments
by keyword, you're probably reading them and assigning by position. You
may not even know what the columns are until you read the CSV file.
Let's think some more about reading from a CSV file. How often do you
have three one-letter column names like "x", "y", "z"? I don't know
about you, but for me, never. I'm more likely to have a dozen columns,
or more, and I can't remember and don't want to remember what order
they're supposed to be *every single time* I read a row or make a tuple
of values.
The point of using keywords is to avoid needing to remember the order.
If I have to remember the order, why bother naming them?
I think this proposal combines the worst of both worlds:
- like positional arguments, you have to care about the order,
and if you get it wrong, your code will likely silently break
in a hard to debug way;
- and like keyword arguments, you have the extra typing of having
to include the field names;
- but unlike keyword arguments, you have to include every single
one, in the right order.
--
Steve
More information about the Python-ideas
mailing list