Skip to content

Instantly share code, notes, and snippets.

@mengzhuo
Created August 9, 2013 01:55
Show Gist options
  • Save mengzhuo/6190556 to your computer and use it in GitHub Desktop.
Save mengzhuo/6190556 to your computer and use it in GitHub Desktop.
Lazy Object Of Python
# encoding: utf-8
__authors__ = (u"Meng Zhuo",)
class SimpleLazyObject(object):
"""Lazy Object impletement of Python
:func: init object or class
:returns: SimpleLazyObject
"""
def __init__(self, func):
if callable(func):
func = func()
self.__dict__['_wrapped'] = func
def __getattr__(self, name):
return getattr(self.__dict__['_wrapped'], name)
def __setattr__(self, name, value):
if name == "_wrapped":
self.__dict__['_wrapped'] = value
else:
setattr(self._wrapped, name, value)
def __delattr__(self, name):
if name == "_wrapped":
raise TypeError('Not allow')
delattr(self._wrapped, name)
def __dir__(self):
return dir(self.__dict__['_wrapped'])
@property
def __members__(self):
return self.__dir__()
def __repr__(self):
return self._wrapped.__repr__()
def __str__(self):
return self.__repr__()
def __unicode__(self):
return self.__repr__()
__getattribute__ = __getattr__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment