One solution could be functools.lazy
, so that:
a = lazy(lambda: 1)
print(type(a)) # lazy
print(a) # 1
print(a + 1) # 2
print(type(a + 1)) # int
Could be an object which implements full set of proxy methods with several clearly defined exceptions.
Your initial example would work:
def log(msg, stamp=lazy(time)):
duration = stamp - t_start
There are libraries for this.
lazyasd
wrapt - has C implementation
But I think it might be worth considering bringing something robust to standard library.