Rebinding __setattr__

Maric Michaud maric at aristote.info
Mon Sep 15 13:12:54 CEST 2008


Le Monday 15 September 2008 01:06:08 Jan Schilleman, vous avez écrit :
> Hi all,
>
> I am trying to redefine __setattr__.
>
> The base class is in a library (actually, it is
> win32com.client.DispatchBaseClass) and I do not want to touch it. My
> problem is exemplified below. To my surprise, __setattr__ and __str__
> behave differently; I can redefine __str__ and the inherited __str__ is
> still the redefined one. But redefining __setattr__ on the base class does
> not get inherited. In Base.__dict__ the __setattr__ is the one I expected,
> but it is not called from B.
>
> What is the problem? Is __setattr__ cached somewhere [and not updated?]?
>

Yes, it doesn't work with classic classes, but do with new-style ones, I don't 
know if this an accepted behavior or not.
Can't you use new-style classes ?


>>>[2]: sys.version
...[2]: '2.5.2 (r252:60911, May 28 2008, 19:19:25) \n[GCC 4.2.4 (Debian 
4.2.4-1)]'


>>>[64]: class A :
    def __setattr__(self, name, value) :
        print "A", name
   ....:
   ....:

>>>[67]: class B(A) : pass
   ....:

>>>[68]: A().c, B().c = 1, 2
A c
A c

>>>[69]: A.__setattr__ = lambda s, n, v : sys.stdout.write("%s, %s, %s\n" % 
(s, n, v))

>>>[70]: A().c, B().c = 1, 2
<__main__.A instance at 0x2b3b41546290>, c, 1
A c





>>>[71]: class A(object) :
    def __setattr__(self, name, value) :
        print "A", name
   ....:
   ....:

>>>[74]: class B(A) : pass
   ....:

>>>[75]: A().c, B().c = 1, 2
A c
A c

>>>[76]: A.__setattr__ = lambda s, n, v : sys.stdout.write("%s, %s, %s\n" % 
(s, n, v))

>>>[77]:

>>>[78]: A().c, B().c = 1, 2
<__main__.A object at 0x2b3b45dfa350>, c, 1
<__main__.B object at 0x2b3b45dfa350>, c, 2

-- 
_____________

Maric Michaud



More information about the Python-list mailing list