This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: slice(None) is slice(None) is False
Type: Stage: resolved
Components: Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: nschloe, steven.daprano
Priority: normal Keywords:

Created on 2021-04-09 08:48 by nschloe, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg390598 - (view) Author: Nico Schlömer (nschloe) Date: 2021-04-09 08:48
I stumbled upon this when dealing with NumPy arrays:
```
slice(None) is slice(None)
```
```
False
```
This came up when trying to check if a variable `a` equals `slice(None)`. The comparison
```
a = slice(None)
a == slice(None)
```
```
True
```
works, but doesn't return a single Boolean if a is a NumPy array, for example.

Perhaps there's another way of finding out if a variable is exactly `slice(None)`, but the failure of `is` seems like a bug.
msg390601 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-04-09 09:57
The behaviour of `is` is correct.

The `is` operator tests for object identity, not equality. The reason that `slice(None) is slice(None)` returns False is that the two calls to the slice function return two different objects.

You say that using the equals operator `==` "doesn't return a single Boolean if a is a NumPy array", that is a design flaw in numpy, and there is nothing we can do about it.

You could try something like this:

def equal(a, b):
    flag = (a == b)
    if isinstance(flag, bool):
        return flag
    else:
        return all(flag)
msg390602 - (view) Author: Nico Schlömer (nschloe) Date: 2021-04-09 10:02
Thanks very much, Steven, for the feedback and the suggestion.
History
Date User Action Args
2022-04-11 14:59:44adminsetgithub: 87952
2021-04-09 10:02:09nschloesetmessages: + msg390602
2021-04-09 09:57:38steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg390601

resolution: not a bug
stage: resolved
2021-04-09 08:48:19nschloecreate