[Tutor] Using __setattr__

Don Arnold Don Arnold" <darnold02@sprynet.com
Sat Apr 19 09:14:02 2003


----- Original Message -----
From: "Tony Cappellini" <tony@tcapp.com>
To: <tutor@python.org>
Sent: Saturday, April 19, 2003 2:37 AM
Subject: [Tutor] Using __setattr__


>
>
> I'm trying to use __setattr__ in  a class I"m writing, but have been
having
> problems getting it to work.
>
> I've looked on Active state and found references to this
> self.__dict__['modified'] = 1
>
> I would like to know more about what 'modified' = 1 does, and when I would
> need use it.

When you set an object attribute, a key is added to the object's internal
dictionary with the value you supply. So the code above is what happens
behind the scenes when you say self.modified = 1. If you override
__setattr__, your function will be called whenever you set an attrbute
through dot notation (self.a = b). To avoid recursion, your method will have
to modify the internal dictionary directly.

> The books I have on Python don't go into much detail on overloading
methods
> like __setattr__
>

Alex Martelli's "Python In A Nutshell" gives a good explanation of Python
classes (both old- and new-style), as well as descriptions of all of the
special methods.

One use for overriding __setattr__ would be to implement read-only
attributes:

class A:
    def __init__(self):
        #since we're overriding __setattr__, we have to modify the internal
        #dictionary directly to set attributes
        self.__dict__['a'] = 10
        self.__dict__['b'] = 20

    def __setattr__(self,key,val):
        if key in ['a','b']:
            raise AttributeError, 'Attempt to set read-only attribute ' +
key
        else:
            self.__dict__[key] = val

a = A()
a.c = 23

print a.a
>> 10

print a.b
>> 20

print a.c
>> 23

a.a = 45
>> Traceback (most recent call last):
>>   File "D:/Python22/tutor/junk041803.py", line 21, in ?
>>     a.a = 45
>>   File "D:/Python22/tutor/junk041803.py", line 10, in __setattr__
>>     raise AttributeError, 'Attempt to set read-only attribute ' + key
>> AttributeError: Attempt to set read-only attribute a

HTH,
Don