Issue 14029: When using setattr identifiers can start with any character (original) (raw)
I am not sure if this is actually a bug.
Given documentation @ http://docs.python.org/release/2.5.2/ref/identifiers.html, the issue is that setattr does not appear to check identifier for naming convention.
See a short example below. Running on windows
sys.version_info(major=2, minor=7, micro=1, releaselevel='final', serial=0) sys.version_info sys.version_info(major=2, minor=7, micro=1, releaselevel='final', serial=0) class Example(): pass example = Example() example.@foo = 4 SyntaxError: invalid syntax setattr(example, '@foo', 'bar') dir(example) ['@foo', 'doc', 'module'] example.@foo SyntaxError: invalid syntax getattr(example, '@foo') 'bar'
It's not a bug. The specification of identifiers refers only to the places where they appear in the language grammar, i.e. what you can put into source code. What parameters objects accept in setattr is an entirely different question. Some objects may check for well-formedness, some objects may accept only a small number of identifiers (e.g. when they use slots), some may accept non-strings as attribute names.