cPickle and __getattr__

Chris Curvey ccurvey at earthlink.net
Sun Aug 29 20:12:18 EDT 2004


Hi all,

I have this program

class Company:
    def __init__(self, revenues, costs):
       self.revenues = revenues
       self.costs = costs

    def __getattr__(self, name):
        if name == 'profits':
            return self.revenues - self.costs

c = Company(100, 75)
print c.revenues
print c.costs
print c.profits

import cPickle
print cPickle.dumps(c)

Everything works fine up until the last line.  If I remove the 
__getattr__ function, then everything works (except "print c.profits"). 
  What is the cPickle class trying to get to that is causing my 
__getattr__ function to be called?

-Chris




More information about the Python-list mailing list