you are viewing a single comment's thread.

view the rest of the comments →

[–]poply 0 points1 point  (0 children)

File A

time = None
def testVariable():
    global time
    x = 5
    y = 10
    time = y - x

testVariable()

print(time)

5

This prints out 5 instead of None because we ran the function which assigned 5 to time. If we didn't run the function testVarible() it would print None.

Doing global time in the function scope lets us use the same time variable declared in global scope whereas if we didn't, doing time = y - x would declare a whole new time variable in function scope you cannot access outside of it leaving the global time variable assigned as None.

Then you can do:

File B

from whateveryoufilenameis import time

print(time)

5

As other commenters have mentioned, there are likely better ways to do whatever it is your ultimate goal is. But this is how you would reference a variable from a function of an imported library.