Im talking about accessing variables in parent scopes. In python you have to explicitly declare the var as nonlocal, but in JS it just works. Now, I'm well aware that this was an active design decision made by both communities, and the "Pythonic" way wouldn't be to use `nonlocal` at all probably, but instead use a class or similar. However, I still consider its presence a wart of the language. (likely only because I am so much more familiar with JS!)
Silly micro example to explain what I'm talking about:
JS:
const countUpFromN = (n) => () => n++
Py:
def countUpFromN(n):
def add():
nonlocal n
n += 1
return n-1
return add
Keep in mind that you only need nonlocal to write, reads will work fine. So you can use a variable a bunch in some scope, then suddenly have things break when you try to simply write to it.
Actually, I think an interesting exercise for evaluating a language could be determining "how much code change do small semantic changes require". For instance, if we were to start with passing a generator (in the "generates a value" sense of the word, not the other one), and try to convert it to passing a counter, how would that look?
JS:
foo(x => () => x)
bar(x => () => x++)
Py:
foo(lambda x: lambda: x)
def countUpFromN(n):
def add():
nonlocal n
n += 1
return n-1
return add
bar(countUpFromN)
Fairly damning to python IMO. I'd be interested to see examples of where small changes in Python require large changes in JS.
Silly micro example to explain what I'm talking about:
JS:
Py: Keep in mind that you only need nonlocal to write, reads will work fine. So you can use a variable a bunch in some scope, then suddenly have things break when you try to simply write to it.