Oli*_*n04 3 python class operation while-loop python-3.x
让我们说:
class Bar:
pass
A = Bar()
while A:
print("Foo!")
Run Code Online (Sandbox Code Playgroud)
然后调用什么操作A来确定while循环?
我尝试过,__eq__但没有做太多.
除非您定义自定义,否则用户定义的对象是真实的__bool__:
>>> class A:
... pass
...
>>> a = A()
>>> if a: print(1)
...
1
>>> class B:
... def __bool__(self):
... return False
...
>>> b = B()
>>> if b: print(1)
...
>>>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
45 次 |
| 最近记录: |