7 ms·
Instead of adding an "intermediate" keyword, I think JavaScript's approach is preferable. That is, > var x = 0 Creates a new variable. > x = 0 Assigns a var
by m0th87 16y ago
Instead of adding an "intermediate" keyword, I think JavaScript's approach is preferable. That is,
> var x = 0
Creates a new variable.
> x = 0
Assigns a variable if it has already been declared in a parent scope, or creates a new global variable otherwise.
Of course, this work break all existing Python code, so it would never happen.
- tptacek 16y agoThis semantic is pretty much anathema to Python, which tries to be explicit and to default conservatively. I'm pretty experienced with each of these languages and I routinely forget to qualify variables with "var" and end up stomping on globals.
- AndyKelley 16y agoI think you're throwing the baby out with the bathwater. Using a keyword like "var" is probably the cleanest way to define where the variable is defined. And we don't need to have the default case be implied global. If you reference a variable which has not been declared with "var", have it crash and die. I think this is the best of both worlds.
- tptacek 16y agoThe only way your argument makes sense is if variables are default-global and tightly scoped only when the keyword is applied. If you adopt that scheme, Python doesn't suffer from the syntactical inconvenience this article talks about. Otherwise, you're making the same point that the article is making, which is that you could introduce a scoping keyword to make it easier to deal with closed-over variables. Either way, I don't care; I'm a Ruby developer now.
- m0th87 16y agoIt's not the same point: you would never have to explicitly tell the interpreter where the variable is (global, local or nonlocal). If you required all variable declarations to use var, the interpreter could automatically determine the scope of the variable when it is referenced or updated. This is cleaner IMO.
- AndyKelley 16y agoHow 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".
- artsrc 16y agoMaybe, but if globals are rare the asymmetry to reduce noise may be worth it.
- bretthoerner 16y agoRight, and nonlocal has been added to Python 3 for just this purpose (and maybe it's in 2.7? I forget).
- deleted 16y ago[deleted]
- WilliamLP 16y ago> is preferable ... > or creates a new global variable You're the first person I read who actually defends this feature of JavaScript. (Crockford lists it in his "terrible parts" I believe.)
- m0th87 16y agoI also dislike a lot of what's supposedly JavaScript's "nicer" parts. I guess I just have bad taste :) I like it because it involves one less keyword in the grammar vs the Python 3 solution. Though tptacek makes a good point (I haven't myself had trouble with that, but I can easily see why others might.)
- jacobolus 16y agoThe common case should be short and implicit, and the uncommon case should be explicit. Here, the common case is wanting to create a new variable inside a function, rather than stomp on a global. Instead, JavaScript forces a programmer to explicitly state something that he wants 95%+ of the time, with a nasty failure case when he forgets.
- orangecat 16y agoor creates a new global variable otherwise That is the worst possible result. Requiring an explicit local or global keyword is reasonable, defaulting to local is reasonable, defaulting to global is insane.
- bhiggins 16y agoI agree! If you say: x = 5 I think there should be a "var x" either locally or globally -- SOMEWHERE -- and actually there should be a compile error if "var x" is missing.
- m0th87 16y agoGood point. Though defaulting to any sort of variable declaration could be problematic. You might be creating a new variable when you think you're updating one. I like AndyKelley's crash-on-default proposal best. This would also help the compiler in optimizing.