Re:WTFPython – Understanding Python through surprising snippets
by Scarblac on Monday December 23, 2019 @06:06PM
(#21868293)
Of course I'm not a good judge, I have been programming Python since 1.4 and my colleagues also all have Python experience.
But at least I like the idea that it gives an error message when confronted with ambiguity. That way it doesn't do something you didn't expect silently. Sadly it only gives it at runtime, not at compile time.
Re:WTFPython – Understanding Python through surprising snippets
by xscott on Tuesday December 24, 2019 @12:46AM
(#21870139)
I'm not sure what your point is about Python 1.4. Maybe it's that you're past the point where these things trip you up.
However, there are plenty of cases where Python won't give an error message too. Make a typo somewhere in the middle of your function, and it'll quietly introduce a new variable instead of letting you know. A rarely used branch of an if-statement could hide this indefinitely. This could also be avoided (or at least mitigated) by requiring variables to be declared explicitly.
Re:WTFPython – Understanding Python through surprising snippets
by h8hawk on Monday December 23, 2019 @03:43PM
(#21867189)
js also doesn't behave as you want:
var a = 9;
function foo() {
console.log(a);
var a = 12;
}
foo();
$node foo.js
undefined
The same goes with ruby.
Re:WTFPython – Understanding Python through surprising snippets
by jakear on Monday December 23, 2019 @05:23PM
(#21868024)
You’re sidestepping the problem. The problem is that Python uses the same syntax for variable declaration and variable assignment, and this can lead to unexpected behavior. That’s far from normal in the programming world, making it more unexpected.
My only real point is that Python conflates variable declaration and variable assignment in a way which initially seems like a friendly time-saver, but which ends up being pretty subtle and confusing until you've learned its quirks. All of that just to avoid declaring your variables (in some fictional version of Python):
Here it would be clear which are declarations + initializations, and which are only assignments. And for just the cost of typing the word "var", the compiler could tell you when you've made typos in your variable names. As an added bonus, you could get rid of the "global" and "nonlocal" keywords.