Python While循环语法

Mat*_*ard 1 python loops while-loop

class Solution: 
    def display(self,head):
        current = head
        while current:
            print(current.data,end=' ')
            current = current.next
Run Code Online (Sandbox Code Playgroud)

你好,我在理解上面的循环时有些困难,AFAIK你需要有一个while循环的条件,所以:

while (stuff) == True:
Run Code Online (Sandbox Code Playgroud)

但上面的代码有:

while current:
Run Code Online (Sandbox Code Playgroud)

这是一样的:

while current == head:
Run Code Online (Sandbox Code Playgroud)

谢谢

Mar*_*mro 7

while current:语法的字面意思while bool(current) == True:.该值将首先转换为bool,然后进行比较True.在蟒蛇everyting转换为bool是True,除非它的None,False零或空集合.

请参阅真值测试部分以供参考.