8000 Add operator.is_none() · Issue #115808 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Add operator.is_none() #115808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rhettinger opened this issue Feb 22, 2024 · 2 comments · Fixed by #115814
Closed

Add operator.is_none() #115808

rhettinger opened this issue Feb 22, 2024 · 2 comments · Fixed by #115814
Labels
type-feature A feature request or enhancement

Comments

@rhettinger
Copy link
Contributor
rhettinger commented Feb 22, 2024

Feature or enhancement

Proposal:

With floating point data, the float('NaN') special value is commonly used as a placeholder for missing values. We provide math.isnan to support stripping out those values prior to sorting or summation:

>>> from math import isnan
>>> from itertools import filterfalse
>>> data = [3.3, float('nan'), 1.1, 2.2]
>>> sorted(filterfalse(isnan, data))
[1.1, 2.2, 3.3]

For non-float data, the None special value is commonly used as a placeholder for missing values. I propose a new function in the operator module analogous to math.isnan():

>>> data = ['c', None, 'a', 'b']
>>> sorted(filterfalse(is_none, data))
['a', 'b', 'c']

This helps fulfill a primary use case for the operator module which is to support functional programming with map(), filter(), etc.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

@rhettinger rhettinger added the type-feature A feature request or enhancement label Feb 22, 2024
ThexXTURBOXx added a commit to ThexXTURBOXx/cpython that referenced this issue Feb 22, 2024
@rhettinger
Copy link
Contributor Author

For convenience, it might also be nice to have is_not_none(). The matches what would be done in a typical list comprehension: cleaned = [x for x in data if x is not None]. Also, it would save the need to know about filterfalse() in itertools module which is less well known that builtin functions. A simple builtin filter() call will suffice: mean(filter(is_not_none, data)).

@AlexWaygood
Copy link
Member

For convenience, it might also be nice to have is_not_none()

Agreed -- I was going to suggest this. It also fits the general pattern of the operator module: we have both is_ and is_not, both truth and not_, etc.

ThexXTURBOXx added a commit to ThexXTURBOXx/cpython that referenced this issue Mar 4, 2024
AA-Turner pushed a commit that referenced this issue Aug 10, 2024
Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
blhsing pushed a commit to blhsing/cpython that referenced this issue Aug 22, 2024
…ython#115814)

Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants
0