__set_attr__

Michael Hudson mwh21 at cam.ac.uk
Thu Jan 25 03:03:49 EST 2001


"Amritansh Raghav" <amritansh at mobilian.com> writes:

> (Newbie alert - its been less than a week)

In which case, maybe you should leave __setattr__ alone for a while.

> Under what circumstances does __set_attr__ get callled? I thought it
> was only if the name wasnt found in the dictionary.

That's __getattr__.

> However I can see my
> function being called even when the name is present. (I print out
> self.__dict__ and also do a self.__dict__.has_key(name) just to make sure
> there isnt a spelling error).
> This is happening within my constructor so I wonder if I need to do
> something special here

You need to do things like

class C:
    def __init__(self):
        self.__dict__['spam'] = 'eggs'
    def __setattr__(self, attr, value):
        if attr != 'spam':
           print "it's not spam!"
        self.__dict__[attr] = value

Cheers,
M.

-- 
  Strangely enough  I saw just such a beast at  the grocery store
  last night. Starbucks sells Javachip. (It's ice cream, but that 
  shouldn't be an obstacle for the Java marketing people.)
                                         -- Jeremy Hylton, 29 Apr 1997



More information about the Python-list mailing list