user1913545 user1913545 - 1 year ago 73
Python Question

Why Python scope my variable outside my function?

i start with Python. I like to try new language. So i've got a "simple" problem about scope and Python.

Here is a recursive function

def foo(myarray)
if myarray == False:
myarray = [[0] * 5 for _ in range(5)]
myarray[0][0] = 1
"some code ..."

myarray = False

I don't want to share my var "myarray" in global env. I juste want Python scope "myarray" only in the function not outside. But Python shared "myarray" as it were a global var. How can restrict the scope to the function ?

Answer Source

Disregarding the myriad of syntax errors, your myarray variable seems to be declared globally and that's why it has global scope?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download