Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
normal_variable = "hello I'm a normal variable"
def lazy_variable():
from random import randint
return "hello I'm a lazy variable and my favorite number is {}".format(randint(0, 1000))
import sys
class ModuleWrapper(object):
def __init__(self, wrapped):
self.wrapped = wrapped
def __getattr__(self, name):
attr = getattr(self.wrapped, name)
if callable(attr):
return attr()
else:
return attr
sys.modules[__name__] = ModuleWrapper(sys.modules[__name__])
if __name__ == '__main__':
import lazy_module
print(lazy_module.normal_variable)
print(lazy_module.lazy_variable)
print(lazy_module.lazy_variable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.