5 ms·
Python doesn't let you write outside of local scope without a special keyword. Exactly the inverse of JS. If you want to write to global state, and you're aware
by MostAwesomeDude 15y ago
Python doesn't let you write outside of local scope without a special keyword. Exactly the inverse of JS. If you want to write to global state, and you're aware that you're writing to global state, then you use the "global" keyword. That's all.
- dools 15y agoThis is the PHP approach as well. I quite like the way PHP 5.3 has implemented bringing locally scope variables into scope for use in a closure.
- js2 15y agoPython implicitly binds to the innermost scope on assignment, so you had the opposite problem when you wanted to reach a nested, non-global scope. Hence the nonlocal keyword added in py3k. http://www.python.org/dev/peps/pep-3104/ http://www.python.org/dev/peps/pep-3104/
- rayiner 15y agoIt still conjures up variables out of thin air. sum = 0 for v in someList: smu += v print sum # prints 0, doh!
- tlb 15y agoExactly. The claim is that implicit local variables are less dangerous than implicit global variables. I've been bitten badly by both. Perl was the worst, where implicit variables were scoped differently according to Larry's philosophy when he implemented it. All for the sake of brevity. Can someone please implement strict mode for Node?
- andrewflnr 15y agoDoes that actually run? I don't have an interpreter handy, but the += operator should refer to `smu` before assigning to it, resulting in a NameError.
- foxylad 15y agoActually no: >>> for v in (1,2,3): ... smu+=v ... Traceback (most recent call last): File "<stdin>", line 2, in <module> NameError: name 'smu' is not defined
- deleted 15y ago[deleted]