Python Assign value to variable during condition in while Loop

前端 未结 4 1159
误落风尘
误落风尘 2020-12-16 11:29

A simple question about Python syntax. I want to assign a value from a function to a variable during the condition for a while loop. When the value returned from the functio

4条回答
  •  庸人自扰
    2020-12-16 11:52

    Python 3.8 pep-0572 now address this case using the new notation :=. Have a look :)

    For example:

    while chunk := file.read(8192):
       process(chunk)
    

提交回复
热议问题