Mailing List Archive

PEP-535 (Rich comparison chaining) Discussion?
Hi all,

PEP 535 was deferred until Python 3.8. I would like to discuss the PEP given that some time has passed, and I _personally_ would benefit from its acceptance.


## A little about me:
As a reasonably long-time Python user (well, 16 years), I have gradually moved from using only the core language to working within the DSLs (NumPy et al.) of the Scientific Python ecosystem. Most recently, I've been using Awkward Arrray (https://github.com/scikit-hep/awkward-1.0), which generalises the regular NumPy array programming paradigm into "jagged" data structures, and was initially aimed at the High Energy Physics (HEP) domain.


## TL;DR of the PEP's Motivations:
Various aspects of NumPy's API have become a "lingua-franca" in the Scientific Python domain, such as strongly-overloaded index operations. One such overload allows "mask arrays" — arrays with boolean entries corresponding to a per-element mask — to select a subset of the array. These mask arrays are conveniently built from the rich comparison operators that Python supports, e.g.
```python
x = np.array(...)
y = x[(2 < x) & (x < 8)]
```

As discussed in PEP 535, the limitation on comparison chaining (namely that it is not possible for rich comparisons) makes the above syntax more clunky than it might otherwise be. The main repercussion of this limitation is lower readability:
- operator precedence of bitwise and/or requires a reasonable number of parentheses (https://docs.python.org/3/reference/expressions.html#operator-precedence)
- repetition of the rich-object (array)

With PEP 535 (and the necessary library changes), this would look more like
```python
x = np.array(...)
y = x[2 < x < 8]
```
This is already well described in the PEP, so I'll leave the example there!


## Community Survey
The Oct 2020 Python Developers Survey (https://www.jetbrains.com/lp/python-developers-survey-2020/) suggests that 62% of respondents use NumPy. Additionally, 37% of respondents list "Simple syntax, syntactic sugar, easy to learn" and 30% list "Easy to write & read code, high-level language" amongst their three most liked features in the Python language (to clarify, these answers may overlap by respondent!)

>From these results, I would make a tentative case that this PEP would have a reasonable impact on the Python community, given that libraries benefitting from this proposal (e.g. NumPy) are used by a "majority" of those surveyed.


## Conclusion
This is a bit of a long opener to this list (apologies!). Is there any interest in pushing this PEP along?

Thanks,
Angus Hollands
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/ET7BPR57Q6MT6TUG4SXPSALRC3P3ZY35/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: PEP-535 (Rich comparison chaining) Discussion? [ In reply to ]
Link to the PEP: https://www.python.org/dev/peps/pep-0535/

On Tue, Aug 31, 2021 at 8:52 AM Angus Hollands <goosey15@gmail.com> wrote:

> Hi all,
>
> PEP 535 was deferred until Python 3.8. I would like to discuss the PEP
> given that some time has passed, and I _personally_ would benefit from its
> acceptance.
>
>
> ## A little about me:
> As a reasonably long-time Python user (well, 16 years), I have gradually
> moved from using only the core language to working within the DSLs (NumPy
> et al.) of the Scientific Python ecosystem. Most recently, I've been using
> Awkward Arrray (https://github.com/scikit-hep/awkward-1.0), which
> generalises the regular NumPy array programming paradigm into "jagged" data
> structures, and was initially aimed at the High Energy Physics (HEP)
> domain.
>
>
> ## TL;DR of the PEP's Motivations:
> Various aspects of NumPy's API have become a "lingua-franca" in the
> Scientific Python domain, such as strongly-overloaded index operations. One
> such overload allows "mask arrays" — arrays with boolean entries
> corresponding to a per-element mask — to select a subset of the array.
> These mask arrays are conveniently built from the rich comparison operators
> that Python supports, e.g.
> ```python
> x = np.array(...)
> y = x[(2 < x) & (x < 8)]
> ```
>
> As discussed in PEP 535, the limitation on comparison chaining (namely
> that it is not possible for rich comparisons) makes the above syntax more
> clunky than it might otherwise be. The main repercussion of this limitation
> is lower readability:
> - operator precedence of bitwise and/or requires a reasonable number of
> parentheses (
> https://docs.python.org/3/reference/expressions.html#operator-precedence)
> - repetition of the rich-object (array)
>
> With PEP 535 (and the necessary library changes), this would look more like
> ```python
> x = np.array(...)
> y = x[2 < x < 8]
> ```
> This is already well described in the PEP, so I'll leave the example there!
>
>
> ## Community Survey
> The Oct 2020 Python Developers Survey (
> https://www.jetbrains.com/lp/python-developers-survey-2020/) suggests
> that 62% of respondents use NumPy. Additionally, 37% of respondents list
> "Simple syntax, syntactic sugar, easy to learn" and 30% list "Easy to write
> & read code, high-level language" amongst their three most liked features
> in the Python language (to clarify, these answers may overlap by
> respondent!)
>
> >From these results, I would make a tentative case that this PEP would
> have a reasonable impact on the Python community, given that libraries
> benefitting from this proposal (e.g. NumPy) are used by a "majority" of
> those surveyed.
>
>
> ## Conclusion
> This is a bit of a long opener to this list (apologies!). Is there any
> interest in pushing this PEP along?
>
> Thanks,
> Angus Hollands
> _______________________________________________
> Python-Dev mailing list -- python-dev@python.org
> To unsubscribe send an email to python-dev-leave@python.org
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-dev@python.org/message/ET7BPR57Q6MT6TUG4SXPSALRC3P3ZY35/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
Re: PEP-535 (Rich comparison chaining) Discussion? [ In reply to ]
On Tue, Aug 31, 2021 at 8:57 AM Angus Hollands <goosey15@gmail.com> wrote:


> Should look more like
> ```python
> x = np.array(...)
> y = x[2 < x < 8]
> ```
> Is there any interest in pushing this PEP along?
>

My personal opinion is, it is worth discussing again. I could see the
benefits this brings to Numpy Arrays.
The PEP itself was deferred based on boolean overload dislike than the
short-circuit or chaining operator preference.

--
Senthil
Re: PEP-535 (Rich comparison chaining) Discussion? [ In reply to ]
On Wed, 1 Sep 2021, 7:18 am Senthil Kumaran, <senthil@uthcode.com> wrote:

> On Tue, Aug 31, 2021 at 8:57 AM Angus Hollands <goosey15@gmail.com> wrote:
>
>
>> Should look more like
>> ```python
>> x = np.array(...)
>> y = x[2 < x < 8]
>> ```
>> Is there any interest in pushing this PEP along?
>>
>
> My personal opinion is, it is worth discussing again. I could see the
> benefits this brings to Numpy Arrays.
> The PEP itself was deferred based on boolean overload dislike than the
> short-circuit or chaining operator preference.
>

While I don't plan to reactivate these PEPs myself, I'd be happy to welcome
a co-author that was keen to champion them (535 doesn't stand alone - it
depends on the "binary if" and "binary else" circuit breaking protocol
proposal in 532).

While I believe 535 is mostly OK, 532 needs some updates to account for:

* the new language grammar (the PEP was written before the switch to pegen)
* the walrus operator (some of the possible future extensions mentioned are
irrelevant now)
* fixing the diagram rendering and ReST mark up

Cheers,
Nick.



> --
> Senthil
>
>
> _______________________________________________
> Python-Dev mailing list -- python-dev@python.org
> To unsubscribe send an email to python-dev-leave@python.org
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-dev@python.org/message/ENJYFXADZXDU7RZBD5RJTPS6ZE7YFNMN/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
Re: PEP-535 (Rich comparison chaining) Discussion? [ In reply to ]
Thanks Nick,

I am strongly advocating for the sentiment of PEP 535, but I have not given strong thought to the implementation details established in PEP 532. I'll read through 532 properly, and come back with some thoughts.
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/NBDMSFG3EHXYZO4CDGSEWVHQMG7YWD35/
Code of Conduct: http://python.org/psf/codeofconduct/