Accessibility of Variable Inside a Function

Hi. Another newbie question here:
The material I am using (python essentials on openedg) says that:

The scope of a function’s parameter is the function itself. The parameter is inaccessible outside the function. With it showing the below example:

def adding(x):
    var = 7
    return x + var

print(adding(4))    # outputs: 11
print(var)    # outputs NameError

However, when I try it on the platform I am using, which is Anaconda Python, it shows output of 11 and 7. Does it have to do with what platform one is using?

Thanks.

What happens when you change that 7 to 713 and run it again?

Oh it now returns 1 for print(var)…is 1 just like a placeholder for error?
why did it output the correct one earlier, but then now succeedingly shows 1 every time i change the var to any value…

So clearly it doesn’t print that function’s local var but another, which you’ve previously assigned somewhere else.

1 Like