在"A:"中对A进行了什么操作?

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__但没有做太多.

Tig*_*kT3 6

除非您定义自定义,否则用户定义的对象是真实的__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)