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

后端 未结 4 1316
無奈伤痛
無奈伤痛 2020-12-11 20:14

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

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-11 20:44

    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.

提交回复
热议问题