Why is there no "list" of reserved word in Python?

I just got an error from an error that would have been prevented if list was a reserved word in Python. (Sure, for my part.)

So why is there no list (or dict or float or any of the types) of the reserved word? It seems easier to add an interpreter error than to try to remember a rule.

(I also know that Eclipse / PyDev has a setting that will remind you of this rule, which may be useful.)

+4
source share
4 answers

only keywords are reserved.

list is not a keyword, but a built-in type like str , set , dict , unicode , int , float , etc.

It makes no sense to reserve every possible built-in type; python is a dynamic language, and if you want to replace built-in types with a local name that has its shadows, you should be able to.

Think of a list , and other types as a pre-imported library of object types; would you expect the defaultdict from collections be saved?

Use a static code analyzer to catch such errors; most IDEs make it easy to integrate them.

+14
source

Take a look at this: http://docs.python.org/2/reference/lexical_analysis.html#keywords

list is just a type and is not reserved (neither is int , float , dict , str ... you get the point).

+2
source

Probably for the same reason that classes do not have private attributes. This is the spirit of Python.

0
source

This is a bit of a matter of opinion, but here is my 2c.

It is very important to make a keyword reserved, because, in essence, this means that you can never use this keyword in code, so he often believed that a good programming language design contains a short list. (perl does not, but then perl has a completely different philosophy for most other programming languages ​​and uses special characters before variables to try to prevent collisions).

In any case, to understand why this is so, think about advanced compatibility. Imagine that the python developers decided that array is such a fundamental concept that they want to make it inline (unthinkable - did this happen with set in, um, python 2.6?). If the built-in functions were automatically reserved, then anyone who previously used the array for something else (even if explicitly imported as from superfastlist import array ), or implemented their own ( numpy did this), would suddenly discover that their code is not there would be work, and they would be very angry.

(In this case, consider whether the word help was reserved - zillion libraries, including argparse, use help as the keyword argument)

0
source

All Articles