Last active
August 29, 2015 14:11
-
-
Save vindolin/6c18a83664638a4de6df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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