Assign variable in while loop condition in Python?

前端 未结 11 1529
名媛妹妹
名媛妹妹 2020-12-08 03:39

I just came across this piece of code

while 1:
    line = data.readline()
    if not line:
        break
    #...

and thought, there must

11条回答
  •  一向
    一向 (楼主)
    2020-12-08 04:26

    This isn't much better, but this is the way I usually do it. Python doesn't return the value upon variable assignment like other languages (e.g., Java).

    line = data.readline()
    while line:
        # ... do stuff ... 
        line = data.readline()
    

提交回复
热议问题