__getattr__ / __setattr__ - infinite recursion!?

Moshe Zadka moshez at math.huji.ac.il
Mon Mar 27 13:08:14 EST 2000


On Mon, 27 Mar 2000, Warren Postma wrote:

> # Class with strict type checking and a single key:
> class zdb_rec:
>     def __init__(self, pattern_dict=None, keyname=None):
>         "create an object with a strict pattern of required values to make
> up a type"
>         self.set_valid(pattern_dict)
               ^^^^^ calls set_valid, when there are no instance  attributes

>     def set_valid(self,pattern_dict):
>         self.attrs = pattern_dict
              ^^^^^ calls __setattr__, when there are no instance attributes

>       # setattr does typesafety check:
>     def __setattr__(self,name,value):
>         "custom __setattr_ with typesafety check"
>         try:
>             if (value == None) and (not self.attrs[name][1]):
>                 self.values[name] = None
                      ^^^^ no attr .values, so __getattr__ is called

>     def __getattr__(self,name):
>         try:
>             return self.values[name]
                     ^^^^ no attr .values, so __getattr__ is called


Set .values to {} early in __init__, and you *might* do fine. Set it 
via __dict__, to avoid __setattr__.
--
Moshe Zadka <mzadka at geocities.com>. 
http://www.oreilly.com/news/prescod_0300.html
http://www.linux.org.il -- we put the penguin in .com





More information about the Python-list mailing list