Skip to content

Instantly share code, notes, and snippets.

@redacted
Created January 29, 2015 11:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redacted/dc905c78395da68d870f to your computer and use it in GitHub Desktop.
Save redacted/dc905c78395da68d870f to your computer and use it in GitHub Desktop.
Lazily initialise python object through horrible __getattr__ hacks
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