Is 'input' a keyword in Python?

Question:

I’m new to Python. I’m writing some code in Sublime and it highlights the word ‘input’

I use it as a variable name and it seems to work, so I wondered whether it may be a keyword in a newer version. (I’m currently using 2.7.5)

Asked By: Zoltán

||

Answers:

No, input is not a keyword. Instead, it is a built-in function.

And yes, you can create a variable with the name input. But please don’t. Doing so is a bad practice because it overshadows the built-in (makes it unusable in the current scope).

If you must use the name input, the convention is to place an underscore after it:

input_ = input()
Answered By: user2555451

input is not a keyword, it’s a function provided by the standard library and included in the builtins module (this module provides globally accessible variables and functions.):

>>> import builtins
>>> input is builtins.input
True

And sure, you can create a variable with the name input. It’s perfectly fine for experienced and intermediate users to do so because they can easily figure that the input name has been re-used.

Use the best name for the content/intent you want to convey. If input is the best then use it (provided you don’t need the builtin), and don’t confuse readers with names like input_ (beginners will wonder whether there’s a special meaning to a trailing underscore)

But if you’re a beginner please don’t re-define builtins, by overshadowing the built-in input (overshadowing a variable makes it unusable in the current scope) you’ll end-up with this error when calling input() later on (in the same scope) and you may struggle to figure out why:

TypeError: 'str' object is not callable

Beginners should instead use another name, preferably not input_ because underscores have special meanings in python, as a result other beginners will wonder whether there’s a special meaning for that trailing underscore (is it the same or related to leading underscores? or maybe to double underscores?)

In another comment someone stated that it is a bad practice to overshadow variables and he even came up with a convention that he borrowed from another use. After all, if overshadowing variables were a really bad practice, the python language designers wouldn’t have allowed it in the first place, but they know and recognize that it has the potential to improve readability, just as it does in other languages. So they allowed it, and it also ease transition to Python from other languages where overshadowing is also allowed like C/C++, Java and even bash.

note: the conventional use for a trailing underscore is where it’s impossible to use a name, like the keyword class in Python. Then you’d use class_ (but like I wrote above, it’s best to avoid it in Python because underscores can confuse beginners as they can convey special meanings)

Answered By: Eric
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.