In jupyter notebook, I commented my variable, however instead of a 'variable is not defined error' I still get value output
Question:
In a jupyter notebook, what is the reason when I commented a variable declaration and trying to call it, instead of getting a ‘variable is not defined error’ , I get the output before I commented that variable?
import random
number = random.randint(1,9)
##user_guess = 3
def first():
print(number)
def second():
print(user_guess)
second()
Output: 3
it should be ‘variable is not defined’
Answers:
You must have run that cell in the notebook with the variable defined before you commented it out and ran the cell again. Jupyter Notebook keeps previously defined variables in memory. To get NameError (“user_guess not defined”), you need to restart the notebook kernel and run the cell again.
The Jupyter runtime stores all variables in memory. For example if in one code cell you were to type
x = 1
the variable x would be accessible in all cells you run after that as it has been stored in the computer’s memory. So in the next cell if you were to type
print(x)
x
from the previous cell would be accessible here.
The same sort of thing is happening here – you have declared x as a variable so it is currently a variable sitting in your computer’s memory. When you rerun the cell with the variable name commented out, the variable is already in your computer’s memory and Jupyter does not ‘undo’ the result of a code cell if you rerun it so x
is not undefined.
Is there a way to clear the memory of those variables/get the error message without always starting a new kernel?
In a jupyter notebook, what is the reason when I commented a variable declaration and trying to call it, instead of getting a ‘variable is not defined error’ , I get the output before I commented that variable?
import random
number = random.randint(1,9)
##user_guess = 3
def first():
print(number)
def second():
print(user_guess)
second()
Output: 3
it should be ‘variable is not defined’
You must have run that cell in the notebook with the variable defined before you commented it out and ran the cell again. Jupyter Notebook keeps previously defined variables in memory. To get NameError (“user_guess not defined”), you need to restart the notebook kernel and run the cell again.
The Jupyter runtime stores all variables in memory. For example if in one code cell you were to type
x = 1
the variable x would be accessible in all cells you run after that as it has been stored in the computer’s memory. So in the next cell if you were to type
print(x)
x
from the previous cell would be accessible here.
The same sort of thing is happening here – you have declared x as a variable so it is currently a variable sitting in your computer’s memory. When you rerun the cell with the variable name commented out, the variable is already in your computer’s memory and Jupyter does not ‘undo’ the result of a code cell if you rerun it so x
is not undefined.
Is there a way to clear the memory of those variables/get the error message without always starting a new kernel?