
On Sun, Feb 14, 2021, 5:46 PM Steven D'Aprano <steve@pearwood.info> wrote:
I'm curious why you say that if and with are "almost" orthogonal concepts? They seem completely orthogonal to me. You can mix and match them in literally every combination:
I mean in a conceptual sense, not as a matter of Python grammar. Grammatically, I can nest 'for' and 'while' inside each other in any combination, but they do "kinda the same thing." I genuinely need to decide sometimes whether to use one or the other for greater clarity in a certain bit of code. I never need to make that decision between 'if' and 'with'. Python wouldn't be that much worse a language if it dropped one of 'for' and 'while'. Of course I don't advocate such, but it's easy to write: for _ in itertools.repeat(None): if something: break Or alternatively, while True: loopvar = next(stuff) The only sense in which I grant "almost" to with/if is that *occasionally* I want to perform an if test on a context object right after it's bound. In that uncommon case, they are slightly non-orthogonal. But the same is true of for/if and other combos.