This is basically a problem of automatic vectorization and parallelization. This is not a new area: if you’re curious about it there is literature about this in the compilers and high performance computing world going back to the 70s. Short answer: it’s quite hard, especially for generic code. It’s achievable to some degree for array-oriented, loop heavy code, but even then with severe limitations often related to the regularity of the logic in the loop bodies, the iteration space, and the use of pointers. There has been some work for Python to do this (eg, numba), but it is largely for array heavy code instead of general Python.