There is no risk, the class __name__
is a writable attribute and it's not used for anything particularly important - just boring stuff like the repr and the help display.
If you try to assign non-string to it, there is a custom error message, which is strong evidence that Python devs had the chance to restrict to valid identifiers here, and they intentionally chose not to:
>>> class A:
... pass
...
>>> A.__name__ = '123Class'
>>> A.__name__ = 123
TypeError: can only assign string to 123Class.__name__, not 'int'
Here is Guido answering a similar question - about whether attribute names should be restricted to valid identifiers:
https://mail.python.org/pipermail/python-dev/2012-March/117441.html
I've spoken out on this particular question several
times before; it is a feature that you can use any arbitrary string
with getattr()
and setattr()
. However these functions should (and do!)
reject non-strings.
The same reasoning applies to class names (which could also be stored as arbitrary strings in a module namespace). An example use-case for this feature: ensuring there's a consistent mapping between a sequence of class names and some external data source, even if the external data collides with Python reserved words or syntax.