ionelmc / python-lazy-object-proxy

A fast and thorough lazy object proxy.
BSD 2-Clause "Simplified" License
247 stars 36 forks source link

Readme example #20

Closed MSeifert04 closed 7 years ago

MSeifert04 commented 7 years ago

The first example in the documentation first throws an AttributeError and when obj.foobar is changed to obj then it just throws an NameError.

Given the sparseness of documentation shouldn't the first (and only?) example work and demonstrate what the package is capable of?

I was thinking about something along the lines of:

import lazy_object_proxy
import time

def expensive_func():
    print('starting calculation')
    time.sleep(2)  # just as example for a very slow computation
    print('finished calculation')
    # create expensive object
    return 10

obj = lazy_object_proxy.Proxy(expensive_func)
# function is called only when object is actually used
print(obj)  # now expensive_func is called

print(obj)  # the expensive_func  isn't called but the result is "remembered".

If you want I can put together a PR but I wasn't sure if it needs some discussion beforehand.

ionelmc commented 7 years ago

Yes, way better. Make a PR.