Skip to content
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

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

Closed
shackenberg opened this issue Jun 5, 2015 · 6 comments
Closed

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

shackenberg opened this issue Jun 5, 2015 · 6 comments
Labels

Comments

@shackenberg
Copy link

@shackenberg shackenberg commented Jun 5, 2015

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

@pv
Copy link
Member

@pv pv commented Jun 5, 2015

numpy.bool_ is a type, not the same thing as Python booleans:

>>> np.bool_
<type 'numpy.bool_'>

An instance of the type then cannot be the same object as True

@njsmith
Copy link
Member

@njsmith njsmith commented 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
#5942.

@njsmith njsmith closed this Jun 5, 2015
@shackenberg
Copy link
Author

@shackenberg shackenberg commented Jun 5, 2015

Sorry for the bad example, better would have been:
print numpy.ones((1), dtype=bool)[0] --> True
print numpy.ones((1), dtype=bool)[0] is True --> False
print numpy.ones((1), dtype=bool)[0] == True --> True

Thanks for the explanation.

@njsmith
Copy link
Member

@njsmith njsmith commented Jun 5, 2015

Ah, yes, it doesn't help that the numpy types like np.bool_ often disguise
themselves as being python built-in types... Which is unfortunate. Arguably
it should repr as "np.bool_(True)". I guess you could open a bug or
discussion about that on the mailing list if you want.
On Jun 5, 2015 1:46 AM, "Ludwig Schmidt-Hackenberg" <
notifications@github.com> wrote:

Sorry for the bad example, better would have been:
print numpy.ones((1), dtype=bool)[0] --> True
print numpy.ones((1), dtype=bool)[0] is True --> False
print numpy.ones((1), dtype=bool)[0] == True --> True

Thanks for the explanation.


Reply to this email directly or view it on GitHub
#5942 (comment).

@jaimefrio
Copy link
Member

@jaimefrio jaimefrio commented 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:

@charris charris added the 53 - Invalid label Jun 5, 2015
@charris
Copy link
Member

@charris charris commented Jun 5, 2015

Changing the repr sounds sensible except that doctests may be an problem...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.