473,729 Members | 2,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rebinding __setattr__

Hi all,

I am trying to redefine __setattr__.

The base class is in a library (actually, it is
win32com.client .DispatchBaseCl ass) 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__(sel f, attr, value):
print 'Base __setattr__'
self.__dict__[attr] = value

def __str__(self):
return "Base"

class A(Base): pass
class B(Base): pass

def __setattr__(sel f, attr, value):
print 'alternative __setattr__'
self.__dict__[attr] = value

def __str__(self):
return "Alternativ e"

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 1424
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 .DispatchBaseCl ass) 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__(sel f, 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.writ e("%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__(sel f, 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.writ e("%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******** *************** *************** @python.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 .DispatchBaseCl ass) 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__(sel f, 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.writ e("%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__(sel f, 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.writ e("%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
1573
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 :",attr,unexpected,self.__dict__ return self.__dict__ class derived(base): def __setattr__(self,attr,key,*unexpected):
0
1510
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 attributes of another. The way it works is when obj1.attr1 is set obj2.attr2 should have it's __setattr__ method called as well. But it ends up giving me a recursion error. The attribute synchronization is shown below:
3
4397
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. >>> object.__setattr__ <slot wrapper '__setattr__' of 'object' objects> >>> object.__getattr__ Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: type object 'object' has no attribute '__getattr__'
3
1604
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 the __setattr__ method work fine. When I attempt to set or get the property it raises the error: "TypeError: _settext() takes exactly two arguments (1 given)". This suggests that the __setattr__ may be conflicting with assignment to the property...
7
2408
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 Bar(object): pass
0
955
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 (anassembly.dll) with version 1.0.0.0. As the assembly was revised later, the current version (which is deployed) is now 2.0.0.0 (functionally, version 1 and 2 are fully compatible).
12
1226
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 (subscriptable iterables, really) and dicts. My init lookslike this: def __init__(self, obj=None): if type(obj).__name__ in 'list|tuple|set|frozenset': self.me =
0
741
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 "here's the really easy way--but I am stuck. I'm | writing an object to proxy both lists (subscriptable iterables, really) and | dicts. |
2
2303
by: mwojc | last post by:
Hi! My class with implemented __getattr__ and __setattr__ methods cannot be pickled because of the Error: ====================================================================== ERROR: testPickle (__main__.TestDeffnet2WithBiases) ---------------------------------------------------------------------- Traceback (most recent call last): File "deffnet.py", line 246, in testPickle cPickle.dump(self.denet, file)
0
8913
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6016
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.