473,386 Members | 1,886 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,386 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 1484

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....
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.