50

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)

Zoltán
  • 21,321
  • 14
  • 93
  • 134
  • 2
    Shouldn't this get downvoted? Putting his exact question into Google comes up with the appropriate doc pages, followed by an explicit site dedicated to answering this question (reserved keywords) – TankorSmash Dec 19 '13 at 00:26
  • 7
    @TankorSmash The fact that it is a built in function doesn't really explain to a newbie like me why it would be highlighted. I come from Java where there are no built in functions. Furthermore, it's not exactly obvious to a newbie like me that sublime thinks it's a method when I give it no parameters or parentheses. – Zoltán Dec 19 '13 at 00:35

2 Answers2

56

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()
wisbucky
  • 33,218
  • 10
  • 150
  • 101
  • Right-o. Will rename it. Any suggestions though? It's an input to a generic filter function :) – Zoltán Dec 18 '13 at 23:37
  • 1
    @Zoltán - Eh, that's really up to you. I've used `inp` a couple of times. –  Dec 18 '13 at 23:39
  • Right. Python won't stop you from changing what a name that normally refers to a built in function refers to, but it's almost always a bad idea. The built in function still exists, and you can refer to it using `__builtins__` if you really do want to actually change what the function name means but then need access to the builtin. – Peter DeGlopper Dec 18 '13 at 23:41
  • 1
    @Zoltán: not sure what you mean by a filter function, but possible generic names for function parameters are `iterable` or `value` according to whether the input is multiple or single. The inputs to the builtin function `filter` are named `function` and `iterable`. – Steve Jessop Dec 18 '13 at 23:47
  • you could use `inputs` as an alternative variable name – wisbucky Oct 01 '21 at 00:27
  • no that "trailing underscore" convention is when we can't use the name at all, like `class` or `def`. There is no convention for overshadowing an existing name, you just made it up using another convention. And it's perfectly fine to shadow an existing name (otherwise the language would prevent it) as long as the code is not destined to beginners. So in a professional environment it's not a problem to locally redefine `id` or `input`. – Eric Nov 22 '22 at 08:28
0

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)

Eric
  • 1,138
  • 11
  • 24