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

Jussi Piitulainen jpiitula at ling.helsinki.fi
Wed Dec 4 12:30:06 CET 2013


rusi writes:
> On Wednesday, December 4, 2013 2:27:28 PM UTC+5:30, Ian wrote:
> > On Tue, Dec 3, 2013 at 11:31 PM, rusi  wrote:
> > > Its a more fundamental problem than that:
> > > It emerges from the OP's second post) that he wants '-' in the
> > > attributes.  Is that all?
> > >
> > > Where does this syntax-enlargement stop? Spaces? Newlines?
> >
> > At non-strings.
> >
> > >>> setattr(foo, 21+21, 42)
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> > TypeError: attribute name must be string, not 'int'
> 
> Not sure what's your point. 
> 
> OP wants attribute identifiers like
> outer_fieldset-inner_fieldset-third_field.
> Say I have a python expression: 
> obj.outer_fieldset-inner_fieldset-third_field
> 
> It can (in the proposed extension) be parsed as above, or as:
> obj.outer_fieldset - inner_fieldset-third_field
> the first hyphen being minus and the second being part of the
> identifier.
> 
> How do we decide which '-' are valid identifier components --
> hyphens and which minus-signs?

I think the OP might be after the JavaScript mechanism where an
attribute name can be any string, the indexing brackets are always
available, and the dot notation is available when the attribute name
looks like a simple identifier. That could be made to work. (I'm not
saying should, or should not. Just that it seems technically simple.)

Hm. Can't specific classes be made to behave this way even now by
implementing suitable underscored methods?



More information about the Python-list mailing list