Skip to content
This repository has been archived by the owner on Jan 25, 2021. It is now read-only.

Deciding between while and for #16

Closed
ablech opened this issue May 8, 2019 · 3 comments
Closed

Deciding between while and for #16

ablech opened this issue May 8, 2019 · 3 comments
Labels

Comments

@ablech
Copy link
Member

ablech commented May 8, 2019

Description

In the config module in line 208 the following construct is used, referring to PEP 479:

while True:
    try:
        raw_line = next(raw_lines)
    except StopIteration:
        return
    
    # do stuff

This seems to be equivalent to

for raw_line in raw_lines:
    # do stuff

Is there a reason, why the while construct should be preferred in such a situation?
Under which circumstances does this apply?

@ablech ablech added the question label May 8, 2019
@goerz
Copy link
Member

goerz commented May 8, 2019

No, that wouldn't work: the loop body can consume more than one raw_line: there's a next(raw_lines) a little farther below. This is because continuation-lines get combined.

I'll add a comment to clarify this.

In general, the easiest way to answer this question besides just looking at the code would be to resolve #6, and test the config file reader to oblivion with the most pathological files you can think of: then you can change the code and test whether it still works.

goerz added a commit that referenced this issue May 8, 2019
@goerz goerz closed this as completed in 927d4a2 May 8, 2019
@ablech
Copy link
Member Author

ablech commented May 8, 2019

Oh, sorry, I overlooked this. Thanks!

@goerz
Copy link
Member

goerz commented May 8, 2019

No reason to be sorry! It’s an unusual construct, which should have an explanation

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants