When defining a python function, I find it hard to debug if I had a typo in a variable name and that variable already exists in the outer scope. I usually use similar names for variables of the same type of data structure, so if that happens, the function still runs fine but just returns a wrong result. So is there a way to prevent python functions to read outer scope variables?
-
3Use more meaningful variable names. – ILostMySpoon May 07 '15 at 17:58
-
and start using classes to divide things up. – MrAlexBailey May 07 '15 at 17:59
-
be careful when you coding, or get a better IDE to help you. – Haifeng Zhang May 07 '15 at 17:59
-
1@ILostMySpoon the variables do have meaningful names. They just have similar names. – qkhhly May 07 '15 at 18:06
3 Answers
You're going against the point of having Scopes at all. We have local and global scopes for a reason. You can't prevent Python from seeing outer scope variables. Some other languages allow scope priority but Python's design principles enforce strong scoping. This was a language design choice and hence Python is the wrong language to try to prevent outer scope variable reading.
Just use better naming methodologies to ensure no confusion, you change up variable names by using the Find-Replace function that most text editors provide.

- 5,262
- 4
- 34
- 58
They say you should avoid usage of global variables if possible. Also, keep in mind, that python has rules on searching variables in the specific order.
So you can avoid checking global variables only if you will delete them, but it makes usage of global variables useless.

- 1
- 1

- 1,826
- 1
- 21
- 25
-
Global variables _are_ [evil](https://softwareengineering.stackexchange.com/questions/148108/why-is-global-state-so-evil) because they create a global state, and that global state can interact with the rest of your code in weird ways, like creating a bug that is only triggered if functions are called in a specific order. [Functional programs](https://en.wikipedia.org/wiki/Functional_programming) are inherently safe because they don't have side effects. That tells us a very important rule: the more "functional" your code is, the less buggy it will be. Global variables are the opposite to that. – Leonardo Jun 15 '23 at 16:21
A python function can only see the global scope of the module it is defined in. As an example, if you run the code:
def accidentalresolveglobal():
try:
return x
except NameError:
print("variable x does not exist")
x=1
print(accidentalresolveglobal())
The function will resolve x from the outer global scope.
But if you place the function definition into a separate module myfuncs.py and then restart your python session and import that module:
import myfuncs
x=1
print(myfuncs.accidentalresolveglobal())
then the function will not be able to resolve x, and you get a NameError exception as you would like.
The key point (as I understand it), is that a function can only see global objects that exist in its own module. So if you place your function definitions into their own module, and import them into your main module, then you won't have a problem where global objects from your main module are accidentally resolved by a function. I'm just learning python, but it seems like placing function definitions into a separate module provides an important degree of encapsulation in order to avoid accidental name collisions.
I recognize this question is 8 years old. I don't know if the implementation of module global scope has changed across python versions.

- 5,960
- 1
- 13
- 21
-
You just explained that a [namespace](https://en.wikipedia.org/wiki/Namespace) is. you should read [the docs](https://docs.python.org/3/tutorial/modules.html). – Leonardo Jun 15 '23 at 16:15
-
Indeed @Leonardo, I explained what a namespace is because the OP and other answers didn't seem to be aware of it. Python scoping / namespaces avoids the problem the OP had. Using modules / namespaces appropriately is a better solution than the accepted answer to use different variable names. – Quentin Jun 15 '23 at 17:29