[Python-ideas] Syntax for key-value iteration over mappings
Steven D'Aprano
steve at pearwood.info
Mon Jul 27 17:55:19 CEST 2015
On Mon, Jul 27, 2015 at 05:30:38PM +0200, Sven R. Kunze wrote:
> On 27.07.2015 13:42, Lennart Regebro wrote:
> >On Mon, Jul 27, 2015 at 4:12 AM, Steven D'Aprano <steve at pearwood.info>
> >wrote:
> >>It's one more special case syntax for beginners to learn. And it really
> >>is a special case: there's nothing about "for k:v in iterable" that
> >>tells you that iterable must have an items() method. You have to
> >>memorise that fact.
> >This I think is a strong argument.
>
> I cannot follow. There is nothing about 'await' that tells me it can
> only be used with coroutines. I need to memorize that fact, too.
Yes, and you need to memorise what "for" loops do, and "len()", etc. But
if you know English, the name is an aid to memory. There's no aid to
memory with a:b syntax, and googling for it will be a pain.
Not everything is important enough to be given its own syntax. That way
leads past Perl and into APL. (At least APL tries to follow standard
mathematical notation, rather than being a collection of arbitrary
symbols.)
`await` gives us a whole lot of new functionality that was hard or
impossible to do before. What does this give us that we couldn't do
before? What's so special about spam.items() that it needs dedicated
syntax for it?
These are not rhetorical questions. If you can answer those positively,
then I'll reconsider my opposition to this. But if the only thing this
syntax gains us is to avoid an explicit call to .items(), then it just
adds unnecessary cruft to the language.
> >What error would you get when it's the wrong type? An attribute error
> >on .items(), or a special SyntaxError "This syntax can only be used on
> >mappings".
>
> I would like such an error. Because it tells me that it is not what I
> wanted. The current methods silently works and I get an error later.
I don't understand you. If you write `for k,v in spam.items()` and spam
has no items method, you get an AttributeError immediately. How does it
silently work?
--
Steve
More information about the Python-ideas
mailing list