473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Rebinding __setattr__

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__)


Sep 14 '08 #1
2 1397
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
Sep 15 '08 #2
That still would require changing the source of the library ... And i'm not
sure if it would have side effects.

"Maric Michaud" <ma***@aristote.infoschreef in bericht
news:ma**************************************@pyth on.org...
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
Sep 15 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Anand | last post by:
class base: def __setattr__(self,attr,key,*unexpected): print "Base Class :",attr,key,unexpected,self.__dict__ self.__dict__ = key def __getattr__(self,attr,*unexpected): print "Base Class...
0
by: Chris Young | last post by:
Operating Ubunutu Linux 5.04 on iMac 333mhz Python 2.4.1 in IDLE 1.1.1 In trying to create a interactive drawing framework in Python I came across the idea of binding attributes of one object to...
3
by: Thomas Heller | last post by:
Just wondering about this behaviour, why is it this way? Python 2.4.2 (#67, Sep 28 2005, 12:41:11) on win32 Type "help", "copyright", "credits" or "license" for more information. >>>...
3
by: L.C. Rees | last post by:
Can custom __setattr__ methods and properties be mixed in new style classes? I experimented with a new style class with both a custom __setattr__ method and a property. Attributes controlled by...
7
by: George Sakkis | last post by:
I was kinda surprised that setting __class__ or __dict__ goes through the __setattr__ mechanism, like a normal attribute: class Foo(object): def __setattr__(self, attr, value): pass class...
0
by: TobiasH | last post by:
Hi everyone, I have a (probably dumb) question in the scope of .Net's version rebinding capabilities: My application (lets call it myapp.exe v1.0.0.0) was compiled referencing an assembly...
12
by: Joshua Kugler | last post by:
OK, I'm sure the answer is staring me right in the face--whether that answer be "you can't do that" or "here's the really easy way--but I am stuck. I'm writing an object to proxy both lists...
0
by: Terry Reedy | last post by:
"Joshua Kugler" <jkugler@bigfoot.comwrote in message news:futgrq$ih6$1@ger.gmane.org... | OK, I'm sure the answer is staring me right in the face--whether that answer | be "you can't do that" or...
2
by: mwojc | last post by:
Hi! My class with implemented __getattr__ and __setattr__ methods cannot be pickled because of the Error: ====================================================================== ERROR:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.