Skip to content

Instantly share code, notes, and snippets.

@vindolin
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vindolin/6c18a83664638a4de6df to your computer and use it in GitHub Desktop.
Save vindolin/6c18a83664638a4de6df to your computer and use it in GitHub Desktop.
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