Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 779
Tris Avatar asked May 29 '15 08:05

Tris


People also ask

Is type a reserved keyword in Python?

It's not a reserved word (a list of which can be found at http://docs.python.org/reference/lexical_analysis.html#keywords ), but it's generally a bad idea to shadow any builtin.

Which is not reserved keyword in Python?

Hence, 'list' is not a Python reserved word.

What is the difference between keywords and reserved words in Python?

The difference between keywords and reserved wordsKeywords have a special meaning in a language, and are part of the syntax. Reserved words are words that cannot be used as identifiers (variables, functions, etc.), because they are reserved by the language.

What happens if you tried using a Python reserved word as a variable name?

Python will raise an error if you try to assign a value to any of these keywords and so you must avoid these as variable names.

Are some names reserved by Python?

There are 33 reserved keywords in Python. This means no object can have the same name as one of these keywords.

Why should we avoid using the reserved word for the variable?

When a reserved word is used as a variable, we will get an error or some other unexpected result.


2 Answers

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.

like image 191
Martijn Pieters Avatar answered Sep 24 '22 00:09

Martijn Pieters


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.

like image 22
Zach P Avatar answered Sep 23 '22 00:09

Zach P