Why is there no natural syntax for accessing attributes with names not being valid identifiers?
python.list at tim.thechases.com
Wed Dec 4 12:25:15 CET 2013
On 2013-12-04 21:33, Chris Angelico wrote:
> I don't think so. What the OP asked for was:
> Or describing it another way: A literal string instead of a token.
> This is conceivable, at least, but I don't think it gives any
> advantage over a dictionary.
In both cases (attribute-access-as-dict-functionality and
attribute-access-as-avoiding-setattr), forcing a literal actually
diminishes Python's power. I like the ability to do
a[key.strip().lower()] = some_value
setattr(thing, key.strip().lower(), some_value)
which can't be done (?) with mere literal notation. What would they
a.(key.strip().lower()) = some_value
(note that "key.strip().lower()" not actually a "literal" that
ast.literal_eval would accept). That's pretty ugly, IMHO :-)
More information about the Python-list