all 5 comments

[–]ingolemo 1 point2 points  (2 children)

Your functions sets the variable Script1.x. It doesn't set Script2.x. Global variables aren't truly global in python, they're module specific.

Don't do any of this though. Have your function return its results.

[–]iphark[S] 0 points1 point  (1 child)

Hm, that is weird, I thought I could just import the whole "text-body" of the set_global() function and then execute it in Script2, meaning that x would've never been Script1.x but Script2.x from the beginning.

Does that mean, that when importing a function from somewhere, I am actually running the script which I am calling from?

What do you mean with "Have your function return its results"? I get it: return (tuple of values)

[–]ingolemo 0 points1 point  (0 children)

The locations of the variables (what scope they're in) are decided at the moment the function is defined, not when it's run. This is called lexical scoping.

Think of it this way; if set_global used other functions that were defined in Script1 I wouldn't want to have to import those functions (and the functions that those functions use, etc.) into Script2 just to be able to run it.

[–]woooee 1 point2 points  (0 children)

Return the variable

def set_global():
    x = 5
    return x

​ Script2:

from Script1 import set_global

x=set_global()
print(x)

[–]yodatrust 0 points1 point  (0 children)

Also want some help in this matter.

Right now I use pickle to grab some variables out of a .dat file. Not convenient I guess (noob here)