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

Piotr Dobrogost p at google-groups-2013.dobrogost.net
Wed Dec 4 15:07:17 EST 2013


On Wednesday, December 4, 2013 6:45:05 AM UTC+1, Tim Roberts wrote:
> 
> It is not "very concise".  It is slightly more concise.
> 
>     x = obj.value1
>     x = dct['value1']
> 
> You have saved 3 keystrokes.  

Actually only 1 as you should have compared these:
x = obj.'value-1'
x = dct['value-1']

Unless we compare with what we have now, which gives 9 (without space) or 10 (with space):
x = obj.'value-1'
x = getattr(obj, 'value-1')

> That is not a significant enough savings to create new syntax.  

Well, 9 characters is probably significant enough saving to create new syntax but saving these characters is only a side effect and is not the most important aspect of this proposal which leads us to the next point.

> Remember the Python philosophy that there ought to be one way to do it.

Funny you use this argument against my idea as this idea comes from following this rule whereas getattr goes against it. Using dot is the main syntax to access attributes. Following this, the syntax I'm proposing is much more in line with this primary syntax than getattr is. If there ought to be only one way to access attributes then it should be dot notation.

Regards,
Piotr



More information about the Python-list mailing list