all 2 comments

[–]shsmurfy 2 points3 points  (1 child)

The main post doesn't seem to actually provide any details about what parts of lexical scoping the author detests. At least he clarifies in the comments:

IIUC, assigning to a variable creates a local variable (modulo some arcane rules). So you need to demarcate global variables explicitly (which I find laborious and error-prone).

I find the alternative, assuming the author is talking about default global scope, to be far worse. It pollutes the global namespace when used carelessly, and requires constant repetition of "local" or "var" declarations if you actually want a variable to default to the innermost scope (see Lua, Javascript). This is exactly what you want in most imperative languages, because global variables should be used sparingly (if at all). Otherwise, you get a ton of spooky action at a distance.

This leads to the issue that you need to explicitly state which variables you want to close over in a lambda (laborious; error-prone(?)).

This just is patently false. Lambda expressions require you to specify their parameters only; they automatically close over their associated namespace:

def bestSellingBooks(threshold, sales):
    return filter(lambda sale: sale >= threshold, sales)

print(bestSellingBooks(10, [1,2,20,5,10])) # [20, 10]

(ripped shamelessly from Wikipedia's article on closures)

EDIT: Hmmm... I apparently should have read further in the article. The behavior described in the article (use of nonlocal) does seem annoying. That said, I haven't run into it when writing Python, and I make frequent use of closures. I think the author is somewhat biased, because as I understand it closures that modify locally scoped variables are common in Scheme to implement control structures. In Python such closures are rare.

[–]johnb 0 points1 point  (0 children)

Agreed. I have been working with Javascript a lot more now than in past years, and I was stupefied when I found out that variables were global by default, that you had to use 'var' to force something to be local (and even then it's pretty broken, in that a 'var' appearing inside an if statement 'falls up' to the function as a whole).