import pickle, cPickle class Thing: def __init__(self,aname): print "Thing.__init__ name=",aname self.name=aname def __getattr__(self, attrname): self.name.trim() # any method on the name attribute..->__getattr__ loop raise AttributeError() def test(pickler): print "Creating t..." t=Thing("myname") try: print "Pickle t..." s=pickler.dumps(t) print "Restore t..." t2=pickler.loads(s) print "SUCCESS!!! name=",t2.name except Exception,x: print "FAIL!!! ",repr(x),str(x) print "***pickle***" test(pickle) print "***cPickle***" test(cPickle)