3 ms·
How is that the only way that my argument makes sense? I'm defining a slightly different Python syntax, in which you always have to use "var" to declare new va
by AndyKelley 16y ago
How is that the only way that my argument makes sense?
I'm defining a slightly different Python syntax, in which you always have to use "var" to declare new variables, otherwise the code breaks. That includes globals. With this structure, normal closure syntax is very clean:
var x = 0
def foo():
var x = 1
def blah():
x = 2
blah()
return x
print(foo()) # prints 2
- tptacek 16y agoAh, that makes sense. Thanks for being patient with my questions.
- AndyKelley 16y agoMy roommate (http://thejoshwolfe.blogspot.com/ http://thejoshwolfe.blogspot.com/) pointed out the flaw: The failure of the "var" idea is this code: def getDataFile(): "returns the string 'data/data.dat'" var name = "data.dat" dir = "data" return os.path.join(dir, name) now you've clobbered the global function "dir" without realizing.
- Raphael_Amiard 16y agoWell no, if you can't declare any variable without 'var', the ' dir= "data" ' line is very explicitly assignment and not a variable declaration. You can still make the mistake but the piece of code would stand out. The problem in javascript is that "a=12" does create a new variable, even if it's a global, and that many people just go through their code declaring only globals. If you did get a syntax error everytime you did that, you'd soon have the habit of putting var in front of every new variable you declare.
- AndyKelley 16y agoThat's true. All things considered, I think I'd rather have the "var" implementation, crash when using an undeclared variable, and even with the ability to clobber globals if you forget to use "var".