0

I have a script that I would like to remove the pre-allocation of variables and move it into another script (function). For example, I would like to take the following portion of code from 'script1.py':

var1 = {}
var2 = {}
var3 = {}

And put this into 'function1.py':

def preallocate_vars():
    var1 = {}
    var2 = {}
    var3 = {}

I can then call the function 'preallocate_vars' via 'function1.py' from 'script1.py' and then continue on my business. Something like:

script1.py

from function1 import preallocate_vars

..
(do some work)
..
preallocate_vars()
..
(do some more work)
..
var1[0] = data1
var2[0] = data2
var3[0] = data3
...

However, when I try something like this, I get an error that 'var1 is not defined' in my script1.py. How can this be fixed?

Miss_Orchid
  • 308
  • 1
  • 8
  • 1
    Why would you need to "preallocate" variables? Either way, `function1` is a separate module from `script1` - they won't share variables. – AKX Aug 24 '22 at 17:22
  • 1
    You'll need to use `function1.varX` to access those variables. – Barmar Aug 24 '22 at 17:25
  • 1
    It would be better to have the function return the variables instead of assigning them. Then you can use `var1, var2, var3 = preallocate_vars()` – Barmar Aug 24 '22 at 17:25

0 Answers0