By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,507 Members | 1,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,507 IT Pros & Developers. It's quick & easy.

cPickle and __getattr__

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Chris Curvey wrote:
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?

Potentially lots of things. But the problem isn't that cPickle is
calling __getattr__, exactly. The problem is that your __getattr__
isn't properly signalling non-existent attributes. You should raise
AttributeError instead of implicitly returning None for which there is
no attribute. Adding "raise AttributeError(name)" to the end of the
definition unbreaks it enough to let pickle work.

Jp
-Chris


Jul 18 '05 #2

P: n/a
>
Potentially lots of things. But the problem isn't that cPickle is
calling __getattr__, exactly. The problem is that your __getattr__
isn't properly signalling non-existent attributes. You should raise
AttributeError instead of implicitly returning None for which there is
no attribute. Adding "raise AttributeError(name)" to the end of the
definition unbreaks it enough to let pickle work.


Ah, that's it. Many thanks!

Jul 18 '05 #3

P: n/a
Chris Curvey wrote:
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


When you use __getattr__, you should always raise an attribute error for
names that you don't handle.

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

Paul

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.