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 22:00:32 CET 2013


On Wednesday, December 4, 2013 2:23:24 PM UTC+1, Roy Smith wrote:
> In article <17gt99hg615jfm7bdid26185884d2pfdkf at 4ax.com>,
> 
>  Tim Roberts <> wrote:
> 
> > Piotr Dobrogost <> wrote:
> 
> > >Attribute access syntax being very concise is very often preferred 
> > >to dict's interface. 
> 
> > It is not "very concise".  It is slightly more concise.
> 
> >     x = obj.value1
> >     x = dct['value1']
> 
> > You have saved 3 keystrokes.  That is not a significant enough savings to
> > create new syntax.  Remember the Python philosophy that there ought to be
> > one way to do it.
> 
> I'll trade typing [ ' ' ] for .  any day.  Easier to type, easier to 
> read.  It's not just the character count, it's that you need to move 
> your fingers off the home row (or, at the very least, a big stretch with 
> your pinkie) to reach the brackets.  I suppose that depends on your 
> particular keyboard layout and typing style/skill.

Very true. Just a remark it's actually trading getattr(o,'x') for o.'x' (saving of 11 keystrokes - don't forget shifts :)) as attribute is quite a different beast then key in a dictionary so comparing this to dict's interface is comparing apples to oranges.

Regards,
Piotr



More information about the Python-list mailing list