Not exactly. For emulating things other than lists, there's __getattribute__
, but unfortunately Python doesn't consider operators like x[y]
or x(y)
to be exactly the same as x.__getitem__(y)
or x.__call__(y)
. Operators like that are attributes of the class, not attributes of the instance, as you can see here.
However, you can take advantage of Python's dynamic nature to effectively eliminate that distinction. If your main concern is to save yourself typing, and to produce less code that needs maintaining, you can do something like this:
class override(object):
def __init__(self, methodName):
self.methodName = methodName
def __get__(self, oself, cls):
oself._load(self.methodName)
return getattr(super(oself.__class__, oself), self.methodName)
class LazyList(list):
def _load(self, name):
print 'Loading data for %s...' % (name,)
for methodName in set(dir(list)) - set(dir(object)):
locals()[methodName] = override(methodName)
You probably don't want to use dir()
in real life, but a suitable fixed list of strings could work as a substitute.