Is there a way to forbid a python function to use any variable except local ones?

Viewed 172

I am currently doing manual python code refactoring.

In order to be sure that I break nothing in the original code by forgetting to correct the instructions I enclose within functions, I want to be sure that the function can have no access to the global variables when I am testing them. What would be the right way to do this, except copying them in a separate module?

Edit:

Just to be clear: I am trying to convert my initial code into something like this:

def big_function(args):      
   def one_small_transformation(args):
       # No one else needs to see this transformation outside the function1

   def second_small_transformation(args):

   ...

   # Block of instructions chaining small transformations

# Other big functions and code making them work together

Sometimes I forget to correct the variable names in my small transforms and code inside small transformations call variables from the large block of instructions.

Unittests: The unittests for big_function are passed; errors pop in when I start editing the code after refactoring. On the current stage of the project, writing unittests for the small transformations looks like an overkill, because they will be completely re-written once the internal logic of the big_function is clear.

1 Answers
Related