3 ms·
> If you want lexical scoping […], you have to ask for it in Perl. That's false. Lexical scoping was added in 1994. One cannot ask for it, it's always "on". >
by bmn__ 6y ago
> If you want lexical scoping […], you have to ask for it in Perl.
That's false. Lexical scoping was added in 1994. One cannot ask for it, it's always "on".
> Python has safer defaults.
The parable of the mote and the beam is very opportune here. The Python defaults do not amount to much.
Python has neither strict variables (demo: https://stackoverflow.com/a/34071474 https://stackoverflow.com/a/34071474) nor lexical scope (demo below). That makes it quite unsafe to program, similar to shell: a simple typo silently causes wrong results instead of being shown as error. The safe features cannot be simply enabled, the usual work-around is to use a programming language that implements these features and compiles down to Python.
----
Demo of lack of lexical scope:
$ python3.8 -c 'for i in range(1,4):
print(i)
print(i)'
1
2
3
3
The variable leaks out.
Compare JS (run-time error):
$ node15 -e '[1,2,3].forEach(i => console.log(i)); console.log(i)'
1
2
3
ReferenceError: i is not defined
Compare Perl (compile-time error):
$ perl -Mv5.32 -E 'for my $i (1..3) { say $i; } say $i;'
Global symbol "$i" requires explicit package name (did you forget to declare "my $i"?) at -e line 1.
Execution of -e aborted due to compilation errors.
- erik_seaberg 6y agoYou’re right that Python only has function scope, not quite lexical scope. But perl -e ' sub foo { $x = 42 } foo; print "\$x is $x\n"; ' quietly creates a global variable. You have to ask for lexical scope, and you have to ask for strict mode to get that error (requiring 5.11 or better does that, but merely having 5.11 does not). Backward compatibility justifies this but devs need to beware this is how the language used to be.