problems with __getattr__ & __setattr__

ScherBi at BAM.com ScherBi at BAM.com
Tue Nov 16 14:24:08 EST 1999


I am having trouble using __getattr__ and __setattr__ in some classes.  I've
boiled it down to the code below.
It dumps core under 1.5.2 on Linux or WinNT.

I think maybe it's looping, if so, how does one go about doing this? 
(I'm assuming it's clear enough what I'm trying to do.)

If I comment out the __gettattr__ operation, it doesn't dump core.  Instead
I get an AttributeError: eggs raised at the 'self.eggs[key] = value' call in
__setattr__.  This is what leads me to belive there's something circular
going on here.


Thanks,
 Bill

-------------------------------------------------------------

class spam:

    def __init__(self):
        self.eggs = {}
    
    def __getattr__(self, key):
        try:
            return self.eggs[key]
        except:
            raise
        
    def __setattr__(self, key, value):
        try:
            self.eggs[key] = value
            return 0
        except:
            raise



if __name__ == '__main__':

    s = spam()
    print 'instance of spam created.' 

------------------------------------------------------------------
 
 
 




More information about the Python-list mailing list