0

I ran below code in python shell and from a python file, I expected the same output but ironically i get NameError: name '_' is not defined error in python file.

>>> 10+2
12
>>> print(_)
12

So i started wondering why python shell stores last expression in _ variable but it is not even defined in python file.

  • 1
    does this answer your question https://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python – Sembei Norimaki Dec 21 '22 at 12:00
  • 2
    Because *"last executed expression"* doesn't have a lot of meaning in an actual file (script)... It has a meaning when running an interactive shell... – Tomerikoo Dec 21 '22 at 12:07

0 Answers0