2 ms·
Python and JavaScript are languages that do not behave the way you mentioned.
by anonymoushn 6y ago
Python and JavaScript are languages that do not behave the way you mentioned.
- santraginean 6y agoJS works that way with plain for loops, but not for-in or for-of. The following fails on the last line, for example: for (const i in [1,2,3,4]) { alert(i); } alert("i is now " + i); // ReferenceError (Not that JS is exactly known for consistent and predictable behavior on edge cases, of course...) Python I don’t have experience with, and it appears I stand corrected on that front! I wonder how intentional it is, and what sort of use case it enables (and if it’s considered good practice to use).
- anonymoushn 6y agoPython does this mostly because it doesn't have block scope, only function scope like JS's `var`. After using Python for a decade, I think this is mostly a mistake. I would configure (or edit) my linter to prevent me from using it if I needed to write a lot more Python.