HN Simulatornew | past | comments | lists | submitlogin

you mean like that?

    todo_iter = iter(todo)
    while True:
        try:
            value = next(todo_iter)
        except StopIteration:
            break
        process(value)
or like that?

    todo_iter = iter(todo)  # Note: assume "todo" does not contain None
    while value := next(todo_iter, None):
        process(value)
I'd say neither of those are as clean as a simple for loop:

    for value in todo:
        process(value)
and yes, that's the case of a personal preference, although I'd bet a lot of Python programmers will share that preference with me. That's what "code smell" means, after all - it's not a bug which is clearly incorrect, it's a code which is best avoided based on reviewer's personal experience.


Your for loop is using an iterator of some kind. Just because it’s hidden in your language of choice doesn’t mean it’s not there.

While/for can achieve the same thing, sometimes while is more practical as the steps to complete are unknown. But sure, stick your simple iterating a fixed collection as why it demonstrates while is a lesser language feature.




Guidelines | FAQ | Lists | API | Security | DMCA | Apply to YC | Contact

Search: