dictionary and __getattr__

Alex Martelli aleax at aleax.it
Thu Sep 6 11:52:31 EDT 2001


"Emile van Sebille" <emile at fenx.com> wrote in message
news:9n81ge$5v8rn$1 at ID-11957.news.dfncis.de...
>
> "Chris Ryland" <cpr at emsoftware.com> wrote in message
> news:8827e15d.0109060614.59036c18 at posting.google.com...
> >
> > Hey, won't the new 2.2 support for extending built-in classes such as
> > dictionaries make this both easy and complete?
> >
> > --Chris Ryland, Em Software
>
> I'm not sure that any level of getattr trickery will be complete.  How
would
> you ever recognize both 1's in:
>
> d = {1:1, '1':'One'}
>
> d.1  #?? what does this mean ??

Oh, that one is easy...:

>>> d['1']='one'
>>> d.1
  File "<stdin>", line 1
    d.1
      ^
SyntaxError: invalid syntax
>>>

The compiler (the parser, actually) complains well before
the class of d can do anything about it.  Python's syntax
is clear and simple: attribute-access syntax is
    <expression> . <identifier>
NOT
    <expression> . <expression>
and there are excellent reasons for that:-).


Alex






More information about the Python-list mailing list