Agreed! Optimizing Python code is indeed really hard, and the lack of const and ability to describe capture semantics don't help.
To be fair, the equivalent in C++ is:
#include <iostream>
int main() {
int i;
i = 1;
auto incr_by_1 = [&](int x) {return x + i;};
i = 4;
auto incr_by_4 = [&](int x) {return x + i;};
std::cout << incr_by_1(0) << " and " << incr_by_4(0) << std::endl;
return 0;
}
which prints "4 and 4". Replace the first [&] with [i] and it prints "1 and 4".
A Python implementation also can't replace incr1 by a single CPU increment instruction because it doesn't know the type of x.
That's still a far cry from being unable to give semantics for Python code without running it.
To be fair, the equivalent in C++ is:
which prints "4 and 4". Replace the first [&] with [i] and it prints "1 and 4".A Python implementation also can't replace incr1 by a single CPU increment instruction because it doesn't know the type of x.
That's still a far cry from being unable to give semantics for Python code without running it.