Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't 'list' a reserved word in Python?

I just got bit by a bug that would have been prevented if list were a reserved word in Python. (Dumbery on my part, to be sure.)

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

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

like image 767
BenDundee Avatar asked Dec 03 '22 00:12

BenDundee


1 Answers

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.

like image 114
Martijn Pieters Avatar answered Dec 24 '22 10:12

Martijn Pieters