[Python-ideas] Coming up with an alternative to PEP 505's None-aware operators

Ethan Furman ethan at stoneleaf.us
Fri Feb 16 11:31:18 EST 2018


On 02/15/2018 11:55 PM, Nick Coghlan wrote:
> On 16 February 2018 at 12:19, rymg19 at gmail.com wrote:

>> I don't know...to me this looks downright ugly and an awkward special case.
>> It feels like it combines reading difficulty of inline assignment with the
>> awkwardness of a magic word and the ugliness of using ?. Basically, every
>> con of the other proposals combined...
>
> Yeah, it's tricky to find a spelling that looks nice without being
> readily confusable with other existing constructs (most notably
> keyword arguments).
>
> The cleanest *looking* idea I've come up with would be to allow
> arbitrary embedded assignments to ordinary frame local variables using
> the "(expr as name)" construct:

-1 to ?it

+1 to (name as expr)

> However, while I think that looks nicer in general, we'd still have to
> choose between two surprising behaviours:
>
> * implicitly delete the statement locals after the statement where
> they're set (which still overwrites any values previously bound to
> those names, similar to what happens with exception clauses)

If we're overwriting locals anyway, don't delete it.  The good reason for
unsetting an exception variable doesn't apply here.

> * skip deleting, which means references to subexpressions may last
> longer than expected (and we'd have the problem where embedded
> assignments could overwrite existing local variables)

Odds are good that we'll want/need that assignment even after the immediate
expression it's used in.  Let it stick around.

--
~Ethan~


More information about the Python-ideas mailing list