Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Lazy(type):
- def __init__(cls, name, bases, dict):
- super(Lazy, cls).__init__(name, bases, dict)
- cls.instance = None
- def check_instance(cls):
- if cls.instance is None:
- if hasattr(cls, 'instantiate'):
- setattr(cls, 'instance', getattr(cls, 'instantiate')())
- else:
- raise Exception('Must implement the instantiate class method!')
- def __getattr__(cls, name):
- cls.check_instance()
- return getattr(cls.instance, name)
- def __getitem__(cls, key):
- cls.check_instance()
- return cls.instance.__getitem__(name)
- def __iter__(cls):
- cls.check_instance()
- return cls.__iter__()
- def __contains__(self, item):
- cls.check_instance()
- return cls.__contains__(item)
Add Comment
Please, Sign In to add comment