[Python-ideas] Inline assignments using "given" clauses

Nick Coghlan ncoghlan at gmail.com
Mon May 14 00:46:00 EDT 2018


On 11 May 2018 at 12:45, Tim Peters <tim.peters at gmail.com> wrote:

> [Nick Coghlan]
> > I've been thinking about this problem, and I think for the If/elif/while
> > cases it's actually possible to allow the "binding is the same as the
> > condition" case to be simplified to:
> >
> >     if command =  pattern.match(the_string):
> >         ...
> >     elif command =  other_pattern.match(the_string):
> >         ...
> >
> >     while data = read_data():
>
> Unless there's some weird font problem on my machine, that looks like
> a single "equals sign".  In which case we'd be reproducing C's
> miserable confusion about whether:
>
>     if (i = 1)
>
> was a too-hastily-typed spelling of the intended:
>
>     if (i == 1)
>
> or whether they were thinking "equals" and typed "=" by mistake.
>
> If so, that would get an instant -1 from any number of core devs, who
> have vivid painful memories of being burned by that in C.  That's not
> just speculation - it came up a number of times in the PEP 572
> threads.
>

I was one of those core devs, and would personally prefer to require that
folks spell the inline binding completely unambiguously as "if i given i =
1:".

However, if the repetition of "i" is considered a deal breaker relative to
":=" (even though the status quo already requires repetition of the target
name in the condition), then I'd prefer to add this shorthand (which folks
can then opt to prohibit in favour of the more explicit form in their style
guides) over adding the cognitive complexity of deciding when to use "i =
1" and when to use "i := 1".

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180514/47d06487/attachment.html>


More information about the Python-ideas mailing list