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

2

9

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.

Dutch answered 29/5, 2015 at 8:53 Comment(2)
Why would they need to be? That'd be a lot of reserved keywords, while sometimes id is a great local name in a function.Esme
I have to agree with you, it would be better if they were reserved. One of the reasons why I prefer C++ is that it feels 'safer' in this respect. A workaround is to use an editor which highlights built-in types differently.Cordey
E
19

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.

Esme answered 29/5, 2015 at 8:58 Comment(0)
L
1

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.

Lardy answered 29/5, 2015 at 9:12 Comment(2)
@poke the keywords link to python is broken.Livery
@Livery My bad, didn’t actually test the link… ^^" FixedHekking

© 2022 - 2024 — McMap. All rights reserved.