472,993 Members | 3,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Recursion with __setattr__

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:

#file: sync.py

def sync(attr1,obj1,obj2,attr2=None):
"""Synchronize attribute access

attr1:
name of attribute to synchronize
attr2:
(optional) attribute name to use when setting second object.
"""
if not attr2: attr2 = attr1
# rename old __setattr__'s
try:
obj1.__old_setattr__ = obj1.__setattr__
obj2.__old_setattr__ = obj2.__setattr__
except AttributeError: pass
# modify mirrored attributes on attribute change
def hook_setattr(inst):
# Hey! Only classes have special methods!
class sethook(inst.__class__):
def __setattr__(self,name,value):
try:
self.__old_setattr__(name,value)
except AttributeError: pass
if name in self.__syncdict__:
entry = self.__syncdict__[name]
for mirror,attr in entry: pass
# recursion error here: setattr(mirror,attr,value)
#sethook.__name__ = inst.__class__.__name__ # magic?
inst.__class__ = sethook
# append synchronization info
try:
obj1.__syncdict__[attr1].append(obj2,attr2)
except AttributeError:
obj1.__syncdict__ = {attr1: [(obj2,attr2)]}
try:
obj2.__syncdict__[attr2].append(obj1,attr1)
except AttributeError:
obj2.__syncdict__ = {attr2: [(obj1,attr1)]}
# make the magic work
hook_setattr(obj1)
hook_setattr(obj2)

When I try to call this like sync('attr',obj,obj2) I get a recursion error.
I could understand this happening if I was calling setattr on itself, but
why the other object?

Any help is greatly appreciated. Also, any pointers on efficiency or easier
ways is also welcome. Thanks in
advance.

Oct 16 '05 #1
0 1474

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...
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: Nathan Harmston | last post by:
Hi, I m trying to implement an object which contains lazy" variables. My idea is to alter the getattr and the setattr methods. However I keep on getting a recursion error. My idea is that the...
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: Stef Mientki | last post by:
hello, I tried to find an easy way to add properties (attributes) to a number of different components. So I wrote a class, from which all these components are derived. By trial and error I...
2
by: mwojc | last post by:
Hi! My class with implemented __getattr__ and __setattr__ methods cannot be pickled because of the Error: ====================================================================== ERROR:...
2
by: Jan Schilleman | last post by:
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....
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.