Why is there no natural syntax for accessing attributes with names not being valid identifiers?

Piotr Dobrogost p at google-groups-2013.dobrogost.net
Fri Dec 6 17:51:20 CET 2013

On Friday, December 6, 2013 3:07:51 PM UTC+1, Neil Cerutti wrote:
> On 2013-12-04, Piotr Dobrogost
> <p at google-groups-2013.dobrogost.net> wrote:
> > On Wednesday, December 4, 2013 10:41:49 PM UTC+1, Neil Cerutti
> > wrote:
> >> not something to do commonly. Your proposed syntax leaves the
> >> distinction between valid and invalid identifiers a problem
> >> the programmer has to deal with. It doesn't unify access to
> >> attributes the way the getattr and setattr do.
> >
> > Taking into account that obj.'x' would be equivalent to obj.x
> > any attribute can be accessed with the new syntax. I don't see
> > how this is not unified access compared to using getattr
> > instead dot...
> I thought of that argument later the next day. Your proposal does
> unify access if the old obj.x syntax is removed.

As long as obj.x is a very concise way to get attribute named 'x' from object obj it's somehow odd that identifier x is treated not like identifier but like string literal 'x'. If it were treated like an identifier then we would get attribute with name being value of x instead attribute named 'x'. Making it possible to use string literals in the form obj.'x' as proposed this would make getattr basically needless as long as we use only variable not expression to denote attribute's name.
This is just casual remark.


More information about the Python-list mailing list