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: Add __bool__ to None
Type: Stage:
Components: Interpreter Core Versions: Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: brett.cannon, daniel.urban, eric.snow, pitrou, python-dev, rhettinger, vstinner
Priority: low Keywords: patch

Created on 2011-07-27 19:26 by rhettinger, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
none_bool.diff rhettinger, 2011-07-27 20:33 Py3.3 patch review
Messages (7)
msg141269 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-07-27 19:26
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.
msg141279 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2011-07-27 22:05
It all sounds good to me. Less magic behind the scenes is good.
msg141280 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2011-07-27 22:25
And this doesn't impact "if x is None", so +1.
msg141281 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-07-27 22:27
I'm not sure why the special case shouldn't be retained. It's obviously there for speed reasons.
msg141282 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-07-27 22:36
> I'm not sure why the special case shouldn't be retained.

We can keep the fast path in PyObject_IsTrue but add a __bool__ method to NoneType.
msg141283 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-07-27 22:37
> > I'm not sure why the special case shouldn't be retained.
> 
> We can keep the fast path in PyObject_IsTrue but add a __bool__ method to NoneType.

That's what I meant.
msg141308 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-07-28 16:55
New changeset ccce01988603 by Raymond Hettinger in branch 'default':
Issue 12647: Add __bool__() method to the None object.
http://hg.python.org/cpython/rev/ccce01988603
History
Date User Action Args
2022-04-11 14:57:20adminsetgithub: 56856
2011-07-28 16:56:42rhettingersetstatus: open -> closed
resolution: fixed
2011-07-28 16:55:29python-devsetnosy: + python-dev
messages: + msg141308
2011-07-28 16:05:32daniel.urbansetnosy: + daniel.urban
2011-07-27 22:37:30pitrousetmessages: + msg141283
2011-07-27 22:36:51vstinnersetnosy: + vstinner
messages: + msg141282
2011-07-27 22:27:58pitrousetnosy: + pitrou
messages: + msg141281
2011-07-27 22:25:17eric.snowsetnosy: + eric.snow
messages: + msg141280
2011-07-27 22:05:40brett.cannonsetnosy: + brett.cannon
messages: + msg141279
2011-07-27 20:33:05rhettingersetfiles: + none_bool.diff
keywords: + patch
2011-07-27 19:26:16rhettingercreate