Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101001

Re: using __getitem()__ correctly

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: using __getitem()__ correctly
Date Wed, 30 Dec 2015 08:35:57 -0700
Lines 47
Message-ID <mailman.76.1451489799.11925.python-list@python.org> (permalink)
References <n60kcc$h89$1@dont-email.me> <mailman.70.1451481087.11925.python-list@python.org> <n60qcp$rtp$2@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de NYEnOPApO7GLuZm/eJ/qkQgLZWGKIWvKcn+aYqzQOiXA==
Return-Path <ian.g.kelly@gmail.com>
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 <n60qcp$rtp$2@dont-email.me>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:101001

Show key headers only | View raw


On Dec 30, 2015 7:46 AM, "Charles T. Smith" <cts.private.yahoo@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].

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

using __getitem()__ correctly "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-30 12:57 +0000
  Re: using __getitem()__ correctly Chris Angelico <rosuav@gmail.com> - 2015-12-31 00:11 +1100
    Re: using __getitem()__ correctly "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-30 14:40 +0000
      Re: using __getitem()__ correctly Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-30 08:35 -0700
        Re: using __getitem()__ correctly "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-30 16:58 +0000
          Re: using __getitem()__ correctly Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-30 13:40 -0700
            Re: using __getitem()__ correctly "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-30 22:54 +0000
              Re: using __getitem()__ correctly "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-30 22:58 +0000
              Re: using __getitem()__ correctly Ben Finney <ben+python@benfinney.id.au> - 2015-12-31 10:13 +1100
                Re: using __getitem()__ correctly "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-30 23:18 +0000
                Re: using __getitem()__ correctly Steven D'Aprano <steve@pearwood.info> - 2015-12-31 10:50 +1100
                Re: using __getitem()__ correctly Ben Finney <ben+python@benfinney.id.au> - 2015-12-31 11:21 +1100
                Re: using __getitem()__ correctly "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-31 11:30 +0000
                Re: using __getitem()__ correctly Ben Finney <ben+python@benfinney.id.au> - 2015-12-31 22:51 +1100
                Re: using __getitem()__ correctly Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-12-31 12:12 +0000
                Re: using __getitem()__ correctly "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-31 13:39 +0000
                Re: using __getitem()__ correctly Steven D'Aprano <steve@pearwood.info> - 2016-01-01 02:03 +1100
                Re: using __getitem()__ correctly "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-31 11:17 +0000
                Re: using __getitem()__ correctly Steven D'Aprano <steve@pearwood.info> - 2016-01-01 01:43 +1100
              Re: using __getitem()__ correctly Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-30 17:31 -0700
                Re: using __getitem()__ correctly "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2015-12-31 12:45 +0000

csiph-web