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
Add __bool__ to None #56856
Comments
Currently bool(None) returns False only because it has a special case in PyObject_IsTrue(). All other types can become False only if they implement __bool__() or __len__(). I propose removing the special case branch and instead adding __bool__ to None. This will simplify the explanation of what bool() does to: "bool(x) always returns True unless the object defines __bool__() to return False or defines __len__() to return a non-zero value". The removal of the special case will slightly slow down tests for "if None", and it will slightly speed-up tests for "if x" where x is something other than True, False, or None. |
It all sounds good to me. Less magic behind the scenes is good. |
And this doesn't impact "if x is None", so +1. |
I'm not sure why the special case shouldn't be retained. It's obviously there for speed reasons. |
We can keep the fast path in PyObject_IsTrue but add a __bool__ method to NoneType. |
That's what I meant. |
New changeset ccce01988603 by Raymond Hettinger in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: