J
Jan Schilleman
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?]?
class Base:
def __setattr__(self, attr, value):
print 'Base __setattr__'
self.__dict__[attr] = value
def __str__(self):
return "Base"
class A(Base): pass
class B(Base): pass
def __setattr__(self, attr, value):
print 'alternative __setattr__'
self.__dict__[attr] = value
def __str__(self):
return "Alternative"
a = A()
b = B()
for i in (1, 2):
print
print 'run', i
print
print Base.__dict__
print A.__dict__
print B.__dict__
print
print 'a:', a
a.a1 = 1
print 'b:', b
b.b1 = 1
setattr(Base, '__setattr__', __setattr__)
setattr(A, '__setattr__', __setattr__)
setattr(Base, '__str__', __str__)
setattr(A, '__str__', __str__)
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?]?
class Base:
def __setattr__(self, attr, value):
print 'Base __setattr__'
self.__dict__[attr] = value
def __str__(self):
return "Base"
class A(Base): pass
class B(Base): pass
def __setattr__(self, attr, value):
print 'alternative __setattr__'
self.__dict__[attr] = value
def __str__(self):
return "Alternative"
a = A()
b = B()
for i in (1, 2):
print 'run', i
print Base.__dict__
print A.__dict__
print B.__dict__
print 'a:', a
a.a1 = 1
print 'b:', b
b.b1 = 1
setattr(Base, '__setattr__', __setattr__)
setattr(A, '__setattr__', __setattr__)
setattr(Base, '__str__', __str__)
setattr(A, '__str__', __str__)