Why Isn't 'list' A Reserved Word In Python?
Solution 1:
Only keywords are reserved.
list
is not a keyword but a built-in type, as are str
, set
, dict
, unicode
, int
, float
, etc.
There is no point in reserving each and every possible built-in type; python is a dynamic language and if you want to replace the built-in types with a local name that shadows it, you should be able to.
Think of list
and the other types as a pre-imported library of object types; you wouldn't expect defaultdict
from collections
to be reserved either?
Use a static code analyzer to catch errors like these; most IDEs let you integrate one with ease.
Solution 2:
Have a look at this: http://docs.python.org/2/reference/lexical_analysis.html#keywords
list
is simply a type and is not reserved (neither is int
, float
, dict
, str
... you get the point).
Solution 3:
Probably for the same reason for which classes dont have private attributes. This is spirit of Python.
Solution 4:
This is a bit of an opinion question, but here's my 2c.
It's a big deal to make a keyword reserved, as essentially it means you can never use that keyword in code, so it's often considered good programming language design to keep the list short. (perl doesn't, but then perl has a completely different philosophy to most other programming languages and uses special signs before variables to try to prevent clashes).
Anyway, to see why this is the case, consider forwards compatibility. Imagine the python developers decide that array
is such a fundamental concept that they want to make it a builtin (not inconceivable - this happened with set
in, um, python 2.6?). If builtins were automatically reserved, then anyone who had previously used array
for something else (even if explicitly imported as a from superfastlist import array
), or implemented their own (numpy
have done this), would suddenly find that their code wouldn't work, and they'd be very irate.
(For that matter, consider if help
was made a reserved word - a zillion libraries, including argparse, use help
as a keyword argument)
Post a Comment for "Why Isn't 'list' A Reserved Word In Python?"