def rv(distFunc): class RV(object): def __init__(self, obj): self.obj = obj; def __hash__(self): return distFunc.__hash__(); def __eq__(self, other): return self.__hash__() == other.__hash__(); def __getitem__(self, key): # Case: x[1] ==> P(X=1) if key.__class__ is int: return distFunc(self.obj, key); # Case: x[1, {'y':2, 'z':3}] ==> P(X=1 | Y=2, Z=3) elif key[1].__class__ is dict: return distFunc(self.obj, key[0], key[1]); else: raise TypeError, """Invalid arguments.\n Valid format: x[1] ==> P(X=1) x[1, {'y':2, 'z':3}] ==> P(X=1 | Y=2, Z=3)"""; return property(RV);