[Python-ideas] PEP 505: None-aware operators: operators ?= and ?? and OR
Paul Moore
p.f.moore at gmail.com
Thu Jul 19 09:01:29 EDT 2018
On 19 July 2018 at 13:30, Jonathan Fine <jfine2358 at gmail.com> wrote:
> Hi
>
>> There is a formatted version of this PEP at
>> https://www.python.org/dev/peps/pep-0505/
>
> I've taken a look at this, and have some comments on the first two
> examples drawn from standard library code. (And a very grateful +10
> for writing a script to find such examples.)
>
> I've started a subthread, just to discuss the ?= and ?? operators. And
> something newish, that I call OR.
>
> FIRST EXAMPLE
> The first example is
> ---
> From bisect.py:
> def insort_right(a, x, lo=0, hi=None):
> # ...
> if hi is None:
> hi = len(a)
> ---
>
> Here, None is a sentinel value. The simpler code
> ---
> hi = hi or len(a)
> ---
> fails when hi is zero (or any other value that is False in the boolean context).
>
> This can be fixed by introducing a new operator OR which is similar to
> 'or' but has the semantics this example requires. Thus, given OR we
> can write
> ---
> hi = hi OR len(a)
> ---
> where (A OR B) returns A if A is not None, otherwise it returns B.
>
> (Recall that (A or B) returns A if bool(A), otherwise it returns B.)
How does (A OR B) differ from the PEP's (A ?? B)?
I don't particularly like it in either form. But ?? at least has the
advantage of being used in other languages.
[...]
> Comments ?? suggestions. For example, would a None-aware AND operator be useful?
I see no value in a None-aware AND. Real use cases are the only
reasonable argument for such a thing, not arguments based on symmetry
(or generalisation). And I doubt I'd find real-world use cases
particularly compelling.
Paul
More information about the Python-ideas
mailing list