如何在 python 中编写的 while 语句中获取 return 值?

How can I get return value within while statement written in python?

我有一个函数可以确定 UpdateStatus:

def detemUpStatus(localTuple, newTuple):
    # some logic...
    # it would return integer which would get processed later
    return int(some number predefined)

如果我们不需要更新,detemUpStatus 会是 return 0,所以我决定将其放在 while 语句中,例如:

while (returnValue = detemUpStatus(lT, nT)):
    # some logic to do process update
    #   with returnValue...

哪个return

while (i = 2):
        ^

SyntaxError: invalid syntax

我是 python 的新手,我认为应该有一种像 C 语言那样优雅的方式。

我认为您无法在 while 子句中进行赋值。一些备选方案:

returnValue = detemUpStatus(lT, nT)
while returnValue:
    ...
    returnValue = detemUpStatus(lT, nT)

或者也许:

while True:
    returnValue = detemUpStatus(lT, nT)
    if not returnValue:
        break
    ...

您不能像 C 那样在 while 中进行赋值,但是您可以使用带有标记值的 iter 的 for 循环来循环直到函数 returns 0 允许您使用循环中返回的状态:

for status in  iter(lambda: detemUpStatus(arg1,arg2), 0):
    # use status