It seems to me that it is not mandatory to pass the variables you want to use inside a function to that function in python.
This code:
def functionWithoutArgs():
print(theList)
theList.append('asdf')
print(theList)
theNewList = theList + ['qwer']
print(theNewList)
return theNewList
theList = ['this', 'is', 'a', 'list']
theLastList = functionWithoutArgs()
print(theLastList)
gives me this output (without errors or warnings):
['this', 'is', 'a', 'list']
['this', 'is', 'a', 'list', 'asdf']
['this', 'is', 'a', 'list', 'asdf', 'qwer']
['this', 'is', 'a', 'list', 'asdf', 'qwer']
I was surprised that no exceptions were raised. This because (it seems to me that) allowing functions to use variables that have not been passed to them obfuscates how that function interacts with other code pretty severely.
As far as I understand the whole point of functions is to break programs into small parts that have their interaction with the rest of the code clearly defined.
Question:
Why does python allow using variables inside a function that has not been passed to that function?
Additional info:
The reason for the excess code in the example is this thread
Use a Variable in a Function Without Passing as an Argument
where the accepted answer claims that using unpassed variables is only allowed when the called function does not modify or rename the variable (as I understood at least), which the above code counterexamples.
I understand that this question is borderline subjective or opinion based as it is a "Why..." question, but I felt that this is so fundamental that there has to be some kind of consensus argument for this. In addition, it would be useful knowledge for many new python programmers.