I don’t consider that lazy evaluation or deferred expression. It would be more appropriate to refer to it as Runtime Code Evaluation.
Currently, we can make use of eval
:
import math
import time
x = 500_000
def factorial(x):
t = time.time()
expr = "math.factorial(x)"
print("math.factorial(x) took:", time.time() - t)
t = time.time()
result = eval(expr, globals(), locals())
print("eval(expr) took:", time.time() - t)
print("Bit length of the result:", result.bit_length())
factorial(x)
But I cannot think of any situation where eval
is necessary. In other words, Python and programming languages, in general, provide all the necessary features to express any possible algorithm.
Also, is this what CPython is actually doing—executing code at runtime?