recursion error using setattr and getattr

Simon Brunning simon at brunningonline.net
Thu Jun 7 07:41:48 EDT 2007


On 6/7/07, Nathan Harmston <ratchetgrid at googlemail.com> wrote:
> 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.

Simplifying radically, you are doing:

    def __setattr__(self, attr, k):
        if attr == "var1":
            self.var1 = k

The last line here sets the var1 attribute, and that goes through your
__setattr__() method again; henct the recursion error. You need to
bypass the  __setattr__() method with something like:

self.__dict__['var1'] = k

-- 
Cheers,
Simon B.
simon at brunningonline.net
http://www.brunningonline.net/simon/blog/
GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues



More information about the Python-list mailing list