Defining “boolness” of a class in python

前端 未结 3 2229
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 17:06

Why doesn\'t this work as one may have naively expected?

class Foo(object):
    def __init__(self):
        self.bar = 3
    def __bool__(self):
        retu         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 18:10

    Because the corresponding special method is called __nonzero__() in Python 2, and not __bool__() until Python 3.

提交回复
热议问题