using __getitem()__ correctly

Ian Kelly ian.g.kelly at gmail.com
Wed Dec 30 10:35:57 EST 2015


On Dec 30, 2015 7:46 AM, "Charles T. Smith" <cts.private.yahoo at gmail.com> wrote:
> As is so often the case, in composing my answer to your question, I discovered
> a number of problems in my class (e.g. I was calling __getitem__() myself!), but
> I'm puzzled now how to proceed.  I thought the way you avoid triggering __getattr__()
> from within that was to use self.__dict__[name] but that doesn't work:
>
>   (PDB)p self.attrs.keys()
>   ['mcc', 'abc']
>   (PDB)p self.attrs.__dict__['abc']
>   *** KeyError: KeyError('abc',)

What leads you to believe that this is triggering a call to
__getattr__? The KeyError probably just means that the key 'abc'
wasn't found in the dict.

> class attrdict(dict):
>     def __init__ (self, name = None):
>         if name:
>             self.update (name)
>         print "attrdict: instantiated: ", name
>
>     # AutoVivification
>     def __getattr__ (self, name):
>         print "attrdict:av:__getattr__: entered for ", name      #, " in ", self
>         #if not name in self.__dict__.keys():
>         if not name in self.keys():

Use the "not in" operator, e.g. "if name not in self.keys()".

>             print "attrdict:av:__getattr__: autovivifying ", name
>             #self.__dict__.__setitem__ (name, self.__class__())
>             #self.__setitem__ (name, self.__class__())
>             self.__setattr__ (name, self.__class__())

No reason to explicitly call __setitem__ or __setattr__ here. I'd
probably just do self[name] = self.__class__()

>         #return self.__getitem__(name)
>         #return self.__dict__.__getitem__(name)
>         return self.__getattribute__ (name)

You shouldn't call __getattribute__ from __getattr__, because
__getattr__ is called from __getattribute__, so this would cause an
infinite loop.

Based on the preceding, you probably want to return the value you just
set in the dict, correct? So just return self[name].



More information about the Python-list mailing list