Is it possible to overload logical and in Python?

Deadly 提交于 2019-12-05 19:59:04

No this is not possible. There is a proposal that adds this functionality but for now, it is rejected.

rkrzr

No, it is not possible. See here.

There is no straight way to do this. You could override __nonzero__ for your objects to modify their truth value.

class Truth:

    def __init__(self, truth):
        self.truth = truth

    def __nonzero__(self):
        return self.truth

t = Truth(True)
f = Truth(False)

print bool(t and t)
print bool(t and f)
print bool(f and f)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!