From 149ca1aebad1501be4a5abceac1504f6237e2794 Mon Sep 17 00:00:00 2001 From: wwflower <106407154+wwflower@users.noreply.github.com> Date: Fri, 27 May 2022 15:32:18 -0500 Subject: [PATCH] Update usage.rst I found this very helpful in cleaning up some legacy code so I thought I'd capture the use case. Document basic use of Proxy and using it for lazy class instantiation. --- docs/usage.rst | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/docs/usage.rst b/docs/usage.rst index 3e6e66d..1cfd791 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -5,3 +5,27 @@ Usage To use lazy-object-proxy in a project:: import lazy_object_proxy + + def heavyweight_function(): + result = "something_big" + return result + + my_big_thing = lazy_object_proxy.Proxy(heavyweight_function) + +To do lazy object initialization:: + + class BloatedClass(object): + def __init__(self): + self.a = AnotherClass() + self.b = {} + + def get_bloated_dict(self): + return self.b + + def bloated_class_init(): + return BloatedClass() + + # This will not instantiate the class + # so to the heavyweight init will not be run yet. + + my_bloated_object = lazy_object_proxy.Proxy(bloated_class_init)