1

I have two modules and I'm trying to modify a global variable in the first module from the second module.

app.py:

import time

glob=0;

def setval(val):
    global glob
    glob = val
    print "glob = "+glob


def bau():
    while(1):
      if(glob):
       print"glob is set"
      else:
       print"glob is unset"
      time.sleep(1)

bau()

start.py:

from app import setval
app.setval(1)

I not able to understand why in start.py the full content of app.py is included and not only the function that I want.

Second I don't understand why by running the first app.py and then start.py, that start.py does not modify the value of the global variable in app.

erip
  • 16,374
  • 11
  • 66
  • 121
catalin
  • 41
  • 1
  • 3
  • 2
    Globals are pretty bad. End of story. Consider `class`es. – erip Aug 08 '16 at 12:06
  • Other than testing some behavior is there a reason you are trying to set a variable in another module? – Leon Aug 08 '16 at 12:07
  • Are you running `app.py` and then also running `start.py`? – PM 2Ring Aug 08 '16 at 12:11
  • 4
    @erip: globals aren't good or bad, they are a tool. You shouldn't be so dogmatic; there is no "end of story" -- sometimes they are the right tool for the job. – Bryan Oakley Aug 08 '16 at 12:12
  • @BryanOakley Sometimes they're the _easy_ tool for the job. If you're relying on globals, good luck testing your code. That's neither here nor there. In any case, I think we can agree that it is (seemingly) unnecessary here to maintain a global. – erip Aug 08 '16 at 12:15
  • 1
    @erip: I never said I rely on globals. I merely said sometimes they are the right tool for the job. I very rarely use them myself, but there are times when they are the right choice. My point is, you shouldn't blindly stick to dogmatic rules. Instead, learn the tools, and learn when to use them, when not to use them, and why. – Bryan Oakley Aug 08 '16 at 12:19
  • @BryanOakley I'd love to chat with you about this further. :) – erip Aug 08 '16 at 12:22

2 Answers2

4

I not able to understand why in start.py the full content of app.py is included and not only the function that I want.

You misunderstand how import works. What it does it actually runs the script you are importing and then binds to things defined inside. If you wish to only import a function then your script is not supposed to do anything other then declarations, i.e. remove bau() line.

So normally you would only declare functions, classes and constants inside your scripts and in one root script you would call them.

Second I don't understand why by running the first app.py and then start.py, that start.py does not modify the value of the global variable in app.

That's because setval() is never reached due to bau() call, i.e. start.py is blocked on import statement.


Side note: I suggest you stop using globals. Wrap everything with functions/classes and pass parameters around. Globals are very hard to control.

freakish
  • 54,167
  • 9
  • 132
  • 169
  • 2
    _i.e. remove `bau()` line_ or put it in the main scope. `if __name__ == "__main__": bau()` – erip Aug 08 '16 at 12:08
  • @erip While it is a valid alternative I think that what OP wants is to actually call `bau()` inside `start.py` after `setval()` call. – freakish Aug 08 '16 at 12:14
0

As suggested by freakish, you can use that approach.

Or if you want to keep it in this format that it's called by different scripts , I suggest you use enviroment variables.

start.py

import os
os.environ['glob_var'] = 'any_variable'

app.py

import os
print os.environ.get('glob_var', 'Not Set')
Community
  • 1
  • 1
harshil9968
  • 3,254
  • 1
  • 16
  • 26