Why is there no natural syntax for accessing attributes with names not being valid identifiers?
Tim Chase
python.list at tim.thechases.com
Wed Dec 4 06:25:15 EST 2013
On 2013-12-04 21:33, Chris Angelico wrote:
> I don't think so. What the OP asked for was:
>
> my_object.'valid-attribute-name-but-not-valid-identifier'
>
> 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
look like?
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 :-)
-tkc
More information about the Python-list
mailing list