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

Piotr Dobrogost p at google-groups-2013.dobrogost.net
Thu Dec 5 00:42:14 CET 2013

On Wednesday, December 4, 2013 11:11:56 PM UTC+1, Terry Reedy wrote:
> The discussion of enlarging the scope of 'identifier' is not relevant as 
> you are not proposing that. In particular, you are not asking that 
> obj.value-1 get the 'value-1' attribute of obj. 


> The discussion of keystrokes is also a side-track.

To great degree, yes. Having said that I find extra 11 keystrokes needed to access some attributes to be a freaking big and unjustifiable number.

> What you are proposing, I believe, is a new grammatical category: 
> attribute-name := identifier or string-literal. This would break the 
> symmetry of the grammatical form identifier '.' identifier and change it 
> to the asymmetrical identifier '.' attribute-name, and that is the 

Nice description.

> To put it another way, how does 'obj' get the non-standard attribute 
> 'value-1', when obj is a module or class? The workaround given above for 
> module attributes will not work for classes.

I'm not sure I see your point. Do you mean that being inside class declaration there's no name that referrs to the current namespace (the way __globals__ refer to module's namespace) thus we can't use proposed syntax to access non-standard attributes from this namespace?

> >> 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.
> Not really. As others have pointed out, getattr is the preferred way to 
> get the value of an attribute when you have an object with attributes 
> and a run-time-only reference to the name in a string variable.

Yes, and I think it's very unfortunate in itself. Attribute access is fundamental operation and it's not accident that "operator" for this action is very concise in many languages being one char only. Having to switch to global function to access attribute in case its name is known only at run time is very awkward both when writing and reading code.

> It would also be the case that "obj.'value' is obj.value", so the 
> proposal *would* add duplication.

This is not a big deal and that's what you get when someone had already decided that in expression a.b, b is treated literally.


More information about the Python-list mailing list