Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: using __getitem()__ correctly Date: Wed, 30 Dec 2015 08:35:57 -0700 Lines: 47 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de NYEnOPApO7GLuZm/eJ/qkQgLZWGKIWvKcn+aYqzQOiXA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '#if': 0.05; '(self,': 0.07; 'none):': 0.07; '"if': 0.09; '__init__': 0.09; 'loop.': 0.09; 'name):': 0.09; 'operator,': 0.09; 'subject:()': 0.09; 'subject:using': 0.09; 'skip:# 20': 0.13; 'def': 0.13; 'explicitly': 0.15; '(name,': 0.16; 'keyerror': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'smith"': 0.16; 'wrote:': 0.16; "shouldn't": 0.18; '2015': 0.20; 'keyerror:': 0.22; 'am,': 0.23; 'dec': 0.23; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'skip:" 20': 0.26; 'entered': 0.27; '(e.g.': 0.27; 'skip:# 10': 0.27; 'message-id:@mail.gmail.com': 0.27; "i'm": 0.30; 'print': 0.30; 'e.g.': 0.30; "i'd": 0.31; 'probably': 0.31; 'skip:s 30': 0.31; 'skip:_ 10': 0.32; 'class': 0.33; 'case,': 0.34; 'received:google.com': 0.35; 'question,': 0.35; 'but': 0.36; 'received:209.85': 0.36; "wasn't": 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'thought': 0.37; 'received:209': 0.38; 'self': 0.38; 'means': 0.39; 'to:addr:python.org': 0.40; 'called': 0.40; 'your': 0.60; 'avoid': 0.61; 'here.': 0.62; '30,': 0.63; 'within': 0.64; 'believe': 0.66; 'discovered': 0.83; 'composing': 0.84; 'dict,': 0.84; 'dict.': 0.84; 'to:name:python': 0.84; 'triggering': 0.84; '***': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=JxybQ+Q8lxTeVWFlPeF3fEPUDs6aBb4KgSEHXBH9y1Y=; b=pRBPP6dlkjUEwQw531Dic2szTjWmZQWYbxVP+oD9fszR7EV9Omt6k7oE4veyBmc5qf zyA4+qASNH5VueQNWAEO7QFoRz29cm8DOrhWIW06an1f2L/pNu0RAmIs2Ui+NVEx9QT2 F6LsPVNaDhPAtNPugV/+ov08JgUWn07nGXPQxD7w0qwxZz95P0KuIUDEvjtQpW1s4e5W YsGn4UvQc0tHuWaMysyvC4ImjGesZVvWZEasjqLJRqLxvmaccW/Yenf7rs0p/bYOy2U0 AF38b4oU3fm2RXF0Q3bUyD/WjQ7kwBOd4fE4L6hQaBW5u11ovjtVuUtUxDhwf7AEEOQ2 0hLw== X-Received: by 10.50.61.177 with SMTP id q17mr60776048igr.68.1451489797043; Wed, 30 Dec 2015 07:36:37 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101001 On Dec 30, 2015 7:46 AM, "Charles T. Smith" 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].