Yes indeed, that's a bug-prone misfeature of Python.
Perl and JavaScript both get default arguments right: They make a new array on each call.
Perl and JavaScript also get the for-loop closures right as well. In Perl, "for (my $n = ...)" and in JavaScript "for (let n = ...)" will both create a new "cell" each time around the loop, so closures work as expected.
(However, MSIE's version of "let" doesn't create a new "cell" each time, and this can be a source of difficult to see event callback bugs; best transpiled out.)
use experimental qw(signatures);
sub plus_one($x = 100) {
return $x + 1;
}
# Says 6.
say plus_one(5);
# Says 101.
say plus_one();
(The subroutine signatures feature is still marked experimental, which is a shame.
I use it in virtually all my code, unless I want pre-5.20 compatibility.)
Perl and JavaScript both get default arguments right: They make a new array on each call.
Perl and JavaScript also get the for-loop closures right as well. In Perl, "for (my $n = ...)" and in JavaScript "for (let n = ...)" will both create a new "cell" each time around the loop, so closures work as expected.
(However, MSIE's version of "let" doesn't create a new "cell" each time, and this can be a source of difficult to see event callback bugs; best transpiled out.)