I was playing around with the _
underscore in the Python interpreter and wanted to try if it has the same behavior in code. I have used the underscore in code as a 'Don't care'-variable, like this:
_, a = someFunction()
And in the interpreter to get the last stored value, like this:
>>> 2 + 2
4
>>> a = _
>>> a
4
Now I tried to execute the following example code:
for i in range(5):
2 + 1
a = _
print (a)
In the interpreter as well as written in a Python script and ran using python underscore.py
.
With the behavior in mind that the _
underscore will save the last stored value, because this it is not formatted as a 'Don't care'-variable, the expected outcome would be 2 + 1 = 3
, making 3
the last stored value, which is then saved into the a
variable with a = _
.
The outcome of the interpreter was the following:
>>> for i in range(5):
... 2 + 1
... a = _
...
3
3
3
3
3
>>> print(a)
3
This outcome works as expected while the outcome of the same code saved in a Python script and ran using python underscore.py
, resulted in a name error:
C:\Users\..\Python files>python underscore.py
Traceback (most recent call last):
File "underscore.py", line 3, in <module>
a = _
NameError: name '_' is not defined
When reading the error, it sounds logic that the _
variable is not defined, but, while it probably has something to do with how Python runs a script, I was just wondering what the difference between these two cases is that makes the outcome result in a somewhat logic answer (when you've been using the interpreter like this for a while) vs a name error?
So don't get me wrong, I do know what the _
symbol does in Python. What I'm asking is why the exact same code behaves differently in the interpreter then when run as a Python program in the terminal?
Single standalone underscore _ is a valid character for a Python identifier, so it can be used as a variable name. According to Python doc, the special identifier _ is used in the interactive interpreter to store the result of the last evaluation. It is stored in the builtin module.
The underscore prefix is meant as a hint to another programmer that a variable or method starting with a single underscore is intended for internal use. This convention is defined in PEP 8. This isn't enforced by Python. Python does not have strong distinctions between “private” and “public” variables like Java does.
Python automatically stores the value of the last expression in the interpreter to a particular variable called "_." You can also assign these value to another variable if you want.
Rules for Python variables: A variable name must start with a letter or the underscore character. A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
For anyone that finds it interesting, with the help of @Chris_Rands I found out that the Python interpreter stores the last used value using sys.displayhook
, more on that here.
sys.displayhook
is called on the result of evaluating an expression entered in an interactive Python session. Meaning that it will only have this behavior in the interpreter and not in a Python script ran in the terminal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With