The Python Oracle

How to overload Python's __bool__ method?

This video explains
How to overload Python's __bool__ method?

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle3

--

Chapters
00:00 Question
00:32 Accepted answer (Score 97)
00:58 Thank you

--

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

Question links:
[defining “boolness” of a class in python]: https://stackoverflow.com/questions/8205...

--

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.)