[Python-ideas] Spelling of Assignment Expressions PEP 572 (was post #4)

Nick Coghlan ncoghlan at gmail.com
Sun Apr 15 03:35:06 EDT 2018

On 15 April 2018 at 13:54, Chris Angelico <rosuav at gmail.com> wrote:
> On Sun, Apr 15, 2018 at 1:08 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
>> === Target first, 'from' keyword ===
>>     while (value from read_next_item()) is not None: # New
>>         ...
>> Pros:
>>   * avoids the syntactic ambiguity of "as"
>>   * being target first provides an obvious distinction from the "as" keyword
>>   * typically reads nicely as pseudocode
>>   * "from" is already associated with a namebinding operation ("from
>> module import name")
>> Cons:
>>   * I'm sure we'll think of some more, but all I have so far is that
>> the association with name binding is relatively weak and would need to
>> be learned
> Cons: Syntactic ambiguity with "raise exc from otherexc", probably not serious.

Ah, I forgot about that usage. The keyword usage is at least somewhat
consistent, in that it's short for:

    _tmp = exc
    _exc.__cause__ from otherexc
    raise exc

However, someone writing "raise (ExcType from otherexc)" could be
confusing, since it would end up re-raising "otherexc" instead of
wrapping it in a new ExcType instance. If "otherexc" was also an
ExcType instance, that would be a *really* subtle bug to try and
catch, so this would likely need the same kind of special casing as
was proposed for "as" (i.e. prohibiting the top level parentheses).

I also agree with Nathan that if you hadn't encountered from
expressions before, it would be reasonable to assume they were
semantically comparable to "target = next(expr)" rather than just
"target = expr".


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list