Why is there no natural syntax for accessing attributes with names not being valid identifiers?
tjreedy at udel.edu
Thu Dec 5 04:21:06 CET 2013
On 12/4/2013 6:42 PM, Piotr Dobrogost wrote:
> On Wednesday, December 4, 2013 11:11:56 PM UTC+1, Terry Reedy wrote:
>> 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.
Given that there is almost no need to ever use operator chars in
attribute names and given that syntax changes have the major undesirable
consequence of backward incompatibility, I find it to be a small and
>> 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.
The module workaround, which I find pretty ugly, is this:
>>> len is __builtins__.len
>>> __globals__ = __import__(__name__)
>>> a = 1
>>> a is __globals__.a
I have not checked that the import trick will work when a module is
imported, but I believe it will.
> 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
Right. Class objects are not created until after the class code runs.
>> 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
> Yes, and I think it's very unfortunate in itself.
Do you prefer obj.__dict__['name'] to getattr(obj, 'name')?
Terry Jan Reedy
More information about the Python-list