Grenko Grenko - 1 year ago 47
Python Question

Python: Sending a variable to another script

I'm new to python and kind of been chucked in the deep end at work.

If i had a script which has creates a list such as below:

test = 'string'

l = []

for i in test:

print l

How would i send that this to another python script?

Answer Source

I'm assuming you want to use the variable l defined in a script called into a second script called

So it would be:

$ cat
def demo():
    some_list = []
    for i in 'string':
         some_list.append( i )
    return list 
from first import demo

some_list = demo()
print some_list 

['s', 't', 'r', 'i', 'n', 'g']

The code in a file is declared as code module. To use it, you have to use:

from module import thing

So in this case the function demo was defined as a function in the module first

In the second file you import it and use it.

You can run arbitrary code in modules and declare variables but it is better if they are placed in functions ( or classes ) as shown above.

I hope this is what you needed.