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

Rotwang sg552 at hotmail.co.uk
Sat Dec 7 01:47:06 CET 2013

On 06/12/2013 16:51, Piotr Dobrogost wrote:
>> [...]
>> 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.

But then every time you wanted to get an attribute with a name known at 
compile time you'd need to write obj.'x' instead of obj.x, thereby 
requiring two additional keystrokes. Given that the large majority of 
attribute access Python code uses dot syntax rather than getattr, this 
seems like it would massively outweigh the eleven keystrokes one saves 
by writing obj.'x' instead of getattr(obj,'x').

More information about the Python-list mailing list