The Python Oracle

How to overload Python's __bool__ method?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Techno Bleepage Open

--

Chapters
00:00 How To Overload Python'S __bool__ Method?
00:20 Accepted Answer Score 99
00:41 Thank you

--

Full question
https://stackoverflow.com/questions/8909...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #methods #overloading #magicmethods

#avk47



ACCEPTED ANSWER

Score 99


You should define __nonzero__() in Python 2.x. It was only renamed to __bool__() in Python 3.x. (The name __nonzero__() actually predates the introduction of the bool type by many years.)