3

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

  • Because they are only function names? – mouviciel Mar 20 '14 at 8:02
  • This is similar for most object oriented languages (especially the dynamic ones and those with the "everything is an object" approach). Keywords are only the core syntax. All the classes, even those that belong to the standard libraries, are nothing special compared to classes written by the user. (and I really do not see why there should be a reason to down vote this question) – thorsten müller Mar 20 '14 at 8:59
  • but why should one be allowed to redeclare such a variable. It's a nightmarish scenario – aWebDeveloper Mar 20 '14 at 9:12
  • 2
    why shouldn't I be allowed to write my own def max or def len for a given class? – user40980 Mar 20 '14 at 15:17
  • 1
    @WebDeveloper: Why shouldn't you be available? Why should the language grammar have to know about standard library functions? Redeclaring definitions is actually an orthogonal concern, but even so, what if I wanted to wrap len with a pass-through logger? – Phoshi Mar 20 '14 at 15:29
6

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.