Member-only story
Lazy Evaluation in Python
The idea of lazy is very helpful and Python already offers this features, which means that the object is evaluated when it is needed, not when it is created.
Some benefits of using it are that reduces the time complexity of an algorithm by discarding the temporary computations and conditionals, allows the language runtime to discard sub-expressions that are not directly linked to the final result of the expression,
allows the programmer to access components of data structures out-of-order after initialization, as long as they are free from any circuits.
The logical expression operators like and,or, and if-then-else are all lazy,
because they don`t need to evaluate all arguments to determine the resulting value.
Also python’s generator expression and generator functions are lazy.
they are so useful for conserving memory usage.
For example we can use range() function which follows the concept of Lazy Evaluation, saves the execution time for larger ranges and we never require all the values at time, so ti saves memory consumption as well.
If this function were eager, it would create all 200 numbers. Since it’s lazy, it only creates numbers as requested.
Note: Python 2.x range() function was eager…