473,322 Members | 1,734 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,322 software developers and data experts.

recursion error using setattr and getattr

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 lazy variable can be stored in a variety of
places, Database, PyTables etc. The lazy variable is a large variable
and so I dont want to hold it in memory all of the time, I d rather
just get it when needed and then store it for future work. Most of the
work will be done using var1 and var2

So ......

class LazyBase(object):
self.var1 = 0
self.var2 = None
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
def __setattr__(self, attr, k):
if attr == "var1":
if self.var1 != k
self.var1 = k
if self.lazy != None
self.lazy = None
elif attr == "var2" :
if self.var2 != k
self.var2 = k
if self.lazy != None
self.lazy = None
else:
raise Exception("Attribute")
class SQLLazy(LazyBase):
def __init__(self, name, table="test_table"):
self.table = table
LazyBase.__init__(name)
self.lazy = None
def __getattr__(self, attr):
if attr == "lazy":
if self.lazy == None: # problem line I think
print "Hit Database"
# use sqlalchemy to get the variable
# set self.lazy = lazy
return self.lazy
else:
return self.lazy
else:
try:
return Lazy.__getattr__(attr)
except:
raise Exception("Attribute Exception")
def __setattr__(self, attr, k):
if attr == "lazy"
raise Exception("Cannot alter lazy")
else:
try:
Lazy.__setattr__(attr, k)
except:
raise Exception("Attribute Exception")

if __name__=='__main__':
spam = SQLLazy("Test")
spam.var2 = 10
print spam.lazy
print spam.lazy
spam.var2 = 5
print spam.lazy

I m expecting the output:
Hit Database
Lazy Variable
Lazy Variable
Hit Database
Lazy Variable

However I just get recursion errors. Does anyone have any ideas how I
can implement this sort of thing through my current method, or is
there a better way to accomplish this.

THanks

Nathan
Jun 7 '07 #1
0 1175

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

Similar topics

1
by: Ray | last post by:
Microsoft Windows XP (C) Copyright 1985-2001 Microsoft Corp. C:\Python22>python setup.py py2exe --icon EXE icon Traceback (most recent call last): File "setup.py", line 7, in ? scripts = ,...
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...
10
by: telesphore4 | last post by:
I want to inherit fresh copies of some class variables. So I set up a metaclass and meddle with the class variables there. Now it would be convenient to run thru a dictionary rather than...
10
by: Paulo da Silva | last post by:
Hi! In a class C, I may do setattr(C,'x',10). Is it possible to use getattr/setattr for variables not inside classes or something equivalent? I mean with the same result as exec("x=10"). ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.