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


Groups > comp.lang.python > #101055

Re: using __getitem()__ correctly

From "Charles T. Smith" <cts.private.yahoo@gmail.com>
Newsgroups comp.lang.python
Subject Re: using __getitem()__ correctly
Date 2015-12-31 13:39 +0000
Organization A noiseless patient Spider
Message-ID <n63b6g$66k$1@dont-email.me> (permalink)
References (7 earlier) <mailman.92.1451517241.11925.python-list@python.org> <56846ddf$0$1601$c3e8da3$5496439d@news.astraweb.com> <mailman.93.1451521331.11925.python-list@python.org> <n633kb$gli$3@dont-email.me> <mailman.106.1451563991.11925.python-list@python.org>

Show all headers | View raw


On Thu, 31 Dec 2015 12:12:43 +0000, Oscar Benjamin wrote:


> When you write x.attr the name 'attr' is looked up on the object x. This
> calls x.__getattribute__('attr'). In turn this checks the dict
> associated with the object x i.e. x.__dict__['attr']. This in turn calls
> x.__dict__.__getitem__('attr'). The lookup of x.__dict__ is special and
> doesn't use the normal __getattribute__ mechanism (otherwise this would
> be an infinite recursion). Generally special attributes (with double
> underscores) are looked up in a different way. If x.__dict__ does not
> have the attribute then the dict associated with the class/type of x is
> checked i.e. x.__class__.__dict__['attr']. The lookup of x.__class__ is
> also special. Failing this the other classes in x.__class__.__mro__ are
> checked i.e. x.__class__.__mro__[1].__dict__['attr']. Once these are
> exhausted x.__getattribute__('attr') falls back on calling
> x.__getattr__('attr').


Very good overview of the steps, thank you.


...> 
> The problem with this as with any dict subclass approach is that a dict
> has a load of methods (.items() .keys() .pop() ...) that will now become
> conflated with your dict keys when you use the attribute access:
> 
>>>> d.items
> <built-in method items of attrdict object at 0x7f2025eab868>


Yeah, this makes it confusing, too.  :)

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