precedence of  vs .
ironfroggy at gmail.com
Fri Aug 15 01:01:37 CEST 2008
On Thu, Aug 14, 2008 at 6:46 PM, Michael Tobis <mtobis at gmail.com> wrote:
> I wrote some code to test the precedence of getitem vs getattr; it
> shows that getitem binds tighter.
> I have been handed some code that relies on the observed behavior.
> However, the Nutshell precedence list claims the opposite. Is the
> Nutshell wrong or am I missing something or is this a bug?
> class a(object):
> def __getitem__(self,item):
> return str(item)[:-1]
> class b(object):
> def __getattr__(self,item):
> return str(item)[::-1]
> Albert = a()
> print Albert['abcd']
> # prints 'abc'
> Barney = b()
> print Barney.abc
> # print 'cba'
> print Barney.Albert['abcd']
> # raises TypeError; tries to evaluate 'treblA'['abcd']
> # I expected 'cba' based on Nutshell 2d Ed p 50
attribute access (foo.bar) binds more tightly than subscripting (foo[bar]).
Read my blog! I depend on your acceptance of my opinion! I am interesting!
Follow me if you're into that sort of thing: http://www.twitter.com/ironfroggy
More information about the Python-list