stevenloria.com

Lazily-evaluated property pattern in Python

Jun 30, 2013

Lazy evaluation is a useful pattern that can improve your code's efficiency in many situations. One example of this is instance attributes that take long to compute:

This approach may cause initialization to take unnecessarily long, especially when you don't always need to access Person#relatives.

A better strategy would be to get relatives only when it's needed (i.e. "lazily"):

In this case, the list of relatives is only computed the first time Person#relatives is accessed. After that, it is stored in Person#_relatives to prevent repeated evaluations.

A perhaps more Pythonic approach would be to use a decorator that makes a property lazy-evaluated

This removes a lot of boilerplate, especially when an object has many lazily-evaluated properties.

tagged in python

Please send comments by email. I welcome your feedback, advice, and criticism.