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

后端 未结 4 1315
無奈伤痛
無奈伤痛 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 21:01

    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)

提交回复
热议问题