[Python-ideas] Syntax for key-value iteration over mappings
Sven R. Kunze
srkunze at mail.de
Mon Jul 27 17:30:38 CEST 2015
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.
> 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 value the fact of seeing an error as soon as possible.
Btw. if the proposed syntax is appropriate is another issue. But I would
love to see an improvement on this field.
> Both are quite incomprehensible unless you know exactly what is going
> on and that this is a shortcut for "fox x,y in foo.items():"
Same goes for .items(). It took some time to internalize this special
case (at least from my perspective).
More information about the Python-ideas
mailing list