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

Ned Batchelder ned at nedbatchelder.com
Tue Dec 3 18:38:21 CET 2013


On 12/3/13 12:14 PM, Piotr Dobrogost wrote:
> Hi!
>
> I find global getattr() function awkward when reading code.
> What is the reason there's no "natural" syntax allowing to access attributes with names not being valid Python identifiers in a similar way to other attributes?
> Something along the line of my_object.'valid-attribute-name-but-not-valid-identifier'?
>
>
> Regards,
> Piotr Dobrogost
>

I don't know the real reason, but I imagine it would be that it would be 
very rarely used.  It would need to be an attribute that isn't a valid 
identifier, and you know the attribute at the time you write the code.

I can see scenarios for needing attributes that aren't identifiers, but 
in many of them you also need to access them through a variable rather 
than literally.

--Ned.




More information about the Python-list mailing list