Guest User

Untitled

a guest
Feb 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. class Lazy(type):
  2. def __init__(cls, name, bases, dict):
  3. super(Lazy, cls).__init__(name, bases, dict)
  4. cls.instance = None
  5.  
  6. def check_instance(cls):
  7. if cls.instance is None:
  8. if hasattr(cls, 'instantiate'):
  9. setattr(cls, 'instance', getattr(cls, 'instantiate')())
  10. else:
  11. raise Exception('Must implement the instantiate class method!')
  12.  
  13. def __getattr__(cls, name):
  14. cls.check_instance()
  15. return getattr(cls.instance, name)
  16.  
  17. def __getitem__(cls, key):
  18. cls.check_instance()
  19. return cls.instance.__getitem__(name)
  20.  
  21. def __iter__(cls):
  22. cls.check_instance()
  23. return cls.__iter__()
  24.  
  25. def __contains__(self, item):
  26. cls.check_instance()
  27. return cls.__contains__(item)
Add Comment
Please, Sign In to add comment