2

Is there any way in Python to forbid the use of global variables inside a function?

For example can this:

def some_function():
    print(foo)

foo = 'bar'
some_function()

instead of printing bar be made to raise a NameError like:

NameError: name 'foo' is not defined

Can you limit the variable scope to that of the function (the inner) and deem the global unreachable?

Could some kind of decorator or something similar be utilized?

Ma0
  • 15,057
  • 4
  • 35
  • 65

0 Answers0