-3

please help me understand why this doesn't work and how to change it. I basically want to be able to query values of a var of program one from program two. So here are the most basic programs:

P1:

import time

gscore = 0

def get_info():
    return gscore


def main():
    global gscore
    score = 0

    while score <10:
        time.sleep(1)
        score +=1
        gscore = score
        print(score)

if __name__ == '__main__':
    main()

P2:

from functest import get_info
print(get_info())

The structure may seem a bit weird but basically I have an existing small python game and want to be able to query the score which is why I adapted it in this way. So P1 obviously counts to ten but P2 always gets 0 as a return value. I feel like I'm making a really stupid mistake here... Thx for help :)

Ezechiel2k
  • 13
  • 3
  • 2
    Because when you `import` from `functest` the method `main` never gets executed? That's the whole *point* of using `if __name__ == '__main__':`. – jonrsharpe Nov 28 '17 at 16:45
  • `global` doesn't make a variable shared between different programs (or even between different invocations of the same program). – Sneftel Nov 28 '17 at 16:45
  • You aren't executing the `get_info` when you import `functest` that's why it will always be `0`. – Cyzanfar Nov 28 '17 at 16:46
  • Aaah ok. So when I have P1 running in the background and then execute P2, I'm not communicating with the already running P1 instance. Got it. So what would be the easiest way to do that? – Ezechiel2k Nov 28 '17 at 17:00

3 Answers3

1

When you import a module in Python you are not executing the content of it. if __name__ == "__main__": is there so that your Python files can act as either reusable modules, or as standalone programs.

If you want to execute the main() function you'll need to explicitly call it.:

from functest import main, get_info
main() # calling the main function in your functest file
print(get_info()) # calling the get_info function

this will return the value you are looking for.

Now, main will be called if you execute the functest.py file:

$ python functest.py
  #=> 9
Cyzanfar
  • 6,997
  • 9
  • 43
  • 81
1

if __name__ == '__main__' is only executed when you run your code form a shell and not when you import your program from another program. To fix this you can modify your program as:

In you P1 modify get_info to this:

def get_info():
    global gscore
    return gscore

In your P2 do this:

from functest import main, get_info
main()
print(get_info())

Also, note that there are better way of doing what you are doing like using a class instead of creating a global variable.

class Score(object):
   def __init__(self, gscore=0):
      self.gscore = gscore

   def get_info(self):
      return self.gscore

   def increment(self, score=0):
      while score < 10:
         score +=1
      self.gscore = score
Amit Tripathi
  • 7,003
  • 6
  • 32
  • 58
  • Thanks for the clarification. But now my problem is this: The way you did it, the P1 instance will count to ten and afterwards I will get a value. But I more or less want P1 to slowly count to ten and P2 to be able at any time to get the current value. (In the real version, P1 is a pygame infinite loop) – Ezechiel2k Nov 28 '17 at 17:07
  • 1
    Well, thats a completely different question as the current approach won't be able to do it as the code works in sync fashion and what you want will require a multi-process program with shared storage for storing the variable. – Amit Tripathi Nov 28 '17 at 17:09
  • I agree with @AmitTripathi, that's another problem that is beyond the scope of your original question. – Cyzanfar Nov 28 '17 at 17:13
  • @Ezechiel2k To do what you are trying to do see this answer: https://stackoverflow.com/questions/47577391/python-calling-method-over-multiple-instances-parallelly/47577661#47577661 – Amit Tripathi Dec 09 '17 at 14:32
0

When you import a function of another program, you are importing the functionality of the program, not the actual instance of the program. What I mean by that is that your program only counts to 10 while it's running, but 10 isn't part of the code, it's a result.

What importing it allows you to do is run the bit of code you imported (In your case, running the get_info code with the gscore = 0 being the gscore that is returned, however, this is a distinct instance of your other program, so even if gscore is increasing in another program that you're running with the main method, your second program has it's own gscore that is 0. Importing is more for when you have a sort of library of functions in a program that you want access to.

What you want is to be able to, run time, read a value that the other program has generated. This can't be done with python variables, but it can be done in many other ways. A common way is to write the score to a file, then read the file in your other program. If you need information on how to read and write files, read this guide here.

Davy M
  • 1,697
  • 4
  • 20
  • 27