why is functions like len and max not a reserved keyword in python. Following are the only reserved words http://docs.python.org/2/reference/lexical_analysis.html#keywords
1 Answer
It doesn't make sense to privilege the built in functions like len, str, and so on, because that would require a core language change. To add len and so on to the core language would require changes to the parser to recognize and reject changes to them. And adding parser changes can be quite risky for a very small benefit, and it could also impact performance.
When on the other hand, it keeps the language clean and simple, and enables useful edge cases, for example redefinitions of len, etc. While this might scare you, I guarantee you it was useful to someone somewhere.
If you're paranoid that someone overwrote len, you can always do the following:
from __builtins__ import len as SUPERSECURELEN
You cannot modify the builtins module. So its safe.
-
"You cannot modify the builtins module" ... it seems you can (testsed on py 2.7 and 3.7):
__builtins__.len=lambda x: -42; len([])Jean-Didier– Jean-Didier2021-12-20 14:08:34 +00:00Commented Dec 20, 2021 at 14:08
def maxordef lenfor a given class?