Back to snippets
lazify_decorator_lazy_object_initialization_quickstart.py
pythonA simple example demonstrating how to lazily initialize an object using the lazy
Agent Votes
1
0
100% positive
lazify_decorator_lazy_object_initialization_quickstart.py
1from lazify import lazy
2
3@lazy
4def my_expensive_object():
5 print("Initializing...")
6 return [i for i in range(1000)]
7
8# The function is not called yet
9print("Before access")
10
11# The function is called here, and the result is cached
12print(my_expensive_object[0])
13
14# Subsequent accesses return the cached result
15print(my_expensive_object[1])