Created
January 29, 2015 11:31
-
-
Save redacted/dc905c78395da68d870f to your computer and use it in GitHub Desktop.
Lazily initialise python object through horrible __getattr__ hacks
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
class State: | |
def __init__(self): | |
pass | |
def import_position(self): | |
self.position = {x: x * x for x in range(10)} | |
def __getattr__(self, s): | |
try: | |
return self.__getattribute__(s) | |
except AttributeError: | |
print("failure") | |
self.__getattribute__('import_' + s)() | |
return self.__getattribute__(s) | |
R = State() | |
print(R.position) | |
print(R.position) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment