Why are Python built-in type names not reserved keywords?

2.5k Views Asked by At

In other words, why does Python allow variable names which are identical to built-in type names such as int, float, list, etc? (C++ built-in type names are all reserved, in comparison). Consider the example

# int = 1
i = 2
if type(i) == int:
    print 'Is int'
else:
    print 'Not int'

The output is "Is int". However, if I uncomment the first line, the output will be "Not int". Obviously my "int" has overridden the built-in type int. That is, in my opinion, potentially dangerous.

2

There are 2 best solutions below

0
On

From a blog post on the History of Python by the Python language designer:

Because you cannot use these as variable or function names anywhere, ever, in any Python program, everyone using Python has to know about all the reserved words in the language, even if they don't have any need for them. For this reason, we try to keep the list of reserved words small, and the core developers hem and haw a lot before adding a new reserved word to the language.

Built-in names are just system provided objects, variables with pre-defined values. You are free to re-define these on a module-by-module or function-by-function basis. Making this (rather large list) of names reserved keywords would go against the above stated philosophy.

Making built-in types and functions reserved keywords would also make it very hard to introduce any new names to that list. Adding to the reserved keyword list has severe consequences for forward compatibility. Imagine adding a color type to the language; every piece of code ever written to handle images would need to be re-written to avoid using that new keyword.

Quoting from that same post again:

[W]hen we do decide to add a new keyword, we start a deprecation campaign at least one release before the new keyword is introduced, warning developers to choose a different name for their variables.

[...]

There's no such concern for built-ins. Code that happens to use the name of a new built-in as a variable or function name will continue to function (as long as you don't also try to use the new built-in in the same function). While we still try to be conservative with the introduction of new built-ins, at least we don't have to worry about breaking working code by merely adding something to the language.

2
On

These are all of the reserved keywords.

When parsing the python code, the Python interpreter uses many techniques to identify what the user wants to do - could include line joining for example if the code contains something as such:

my_list = [1,
           2,
           3]

In general, Lexical Analysis: The keywords help the Python interpreter to understand the programmer, by him 'playing by the rules'. You wouldn't (and couldn't) name a variable for because when Python interpreter reads that word - it understands that there is a certain loop involved, same for class (used for classes declaration) and same for all of the keywords (they all perform a specific role in our program).

Why built-in types aren't reserved words? Because that's not how Python interpreter works, it has no trouble understanding you're assigning a value to a variable at a certain moment.

More information (aside from the links given) can be found here.