Why is there no natural syntax for accessing attributes with names not being valid identifiers?
neilc at norwich.edu
Wed Dec 4 22:41:49 CET 2013
On 2013-12-04, Piotr Dobrogost <p at google-groups-2013.dobrogost.net> wrote:
> On Wednesday, December 4, 2013 2:06:44 AM UTC+1, Tim Chase wrote:
>> I think random832 is saying that the designed purpose of setattr()
>> was to dynamically set attributes by name, so they could later be
>> accessed the traditional way; not designed from the ground-up to
>> support non-identifier names. But because of the getattr/setattr
>> machinery (dict key/value pairs), it doesn't prevent you from having
>> non-identifiers as names as long as you use only the getattr/setattr
>> method of accessing them.
> Right. If there's already a way to have attributes with these
> "non-standard" names (which is a good thing)
At best its a neutral thing. You can use dict for the same
purpose with very little effort and no(?) loss of efficiency.
> then for uniformity with dot access to attributes with
> "standard" names there should be a variant of dot access
> allowing to access these "non-standard" named attributes, too.
New syntax needs more than theoretical justifications; it needs
persuasive use cases.
Using invalid identifiers as attributes is generally a bad idea,
not something to do commonly. Your proposed syntax leaves the
distinction between valid and invalid identifiers a problem the
programmer has to deal with. It doesn't unify access to
attributes the way the getattr and setattr do.
More information about the Python-list