3 ms·
I can't think of a single case where using sentinel values is necessary or appropriate in Python. Generally speaking, one should just return from within the lo
by LyndsySimon 12y ago
I can't think of a single case where using sentinel values is necessary or appropriate in Python.
Generally speaking, one should just return from within the loop.
- agentultra 12y agoFunky OS of FFI APIs... but Python has a nice way of abstracting away the pattern using the second argument to iter().
- jessaustin 12y agoI get what you're saying, but even when sentinels are used inside a function, returning a -1 to the caller seems like a pretty bad API. It's OK to raise ValueError! I had thought the idiomatic sentinel value was an instance of object() you could is against, anyway.
- LyndsySimon 12y agoI agree - especially when you're returning a list index, since `[-1]` is a valid index in Python.