question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`numpy.bool_(True) is True` evaluates to `False`

See original GitHub issue

Even though numpy.bool_(True) == True evaluates to True. Doesn’t this violate the PEP8 recommendation that ``Comparisons to singletons like None should always be done with is or is not, never the equality operators.`?

Sorry, if this is a dupe. I searched for bool comarison and pep8 and could not find an relevant issue. numpy.__version__: 1.9.0.dev-297f54b

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
jaimefriocommented, Jun 5, 2015

Actually, the PEP8 recommendation for True and False is that you don’t compare them against anything, not with ==, and especially not with is, but use them directly. The following is the relevant paragraph:

Don’t compare boolean values to True or False using == .

Yes: if greeting: No: if greeting == True: Worse: if greeting is True:

2reactions
njsmithcommented, Jun 5, 2015

The pep recommendation doesn’t mean that nothing is allowed to compare == to singletons, just that if you’re checking for the literal singleton True object you should use “is” to do that. And np.bool_(True) is a different object than True – it has to be, it’s a different class! – so “is” will return False no matter what pep 8 says.

See also: 1 == True, 1 is True On Jun 5, 2015 12:53 AM, “Ludwig Schmidt-Hackenberg” < notifications@github.com> wrote:

Even though numpy.bool_(True) == True evaluates to True. Doesn’t this violate the PEP8 http://legacy.python.org/dev/peps/pep-0008/#other-recommendations recommendation that `Comparisons to singletons like None should always be done with is or is not, never the equality operators.?

Sorry, if this is a dupe. I searched for bool comarison and pep8 and could not find an relevant issue. numpy.version: 1.9.0.dev-297f54b

— Reply to this email directly or view it on GitHub https://github.com/numpy/numpy/issues/5942.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why do "Not a Number" values equal True when cast as ...
1. My hunch: NaN is not equal to zero, so it's true when converted to boolean. If NaN were false, then conversion of...
Read more >
Python and numpy Bool Types
~False in fact evaluates to True. If you want to correctly negate Python boolean values use logical not and not bitwise not (...
Read more >
Python Booleans: Optimize Your Code With Truth Values
You can think of True and False as Boolean operators that take no inputs. One of these operators always returns True , and...
Read more >
Python Booleans - W3Schools
Booleans represent one of two values: True or False . Boolean Values. In programming you often need to know if an expression is...
Read more >
Logic functions — NumPy v1.24 Manual
Test whether all array elements along a given axis evaluate to True. ... Test element-wise for NaN and return result as a boolean...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found