0

I am a newbie to python.I am trying to access a variable defined inside main() in a module which is being imported in the main function. I want a method to get this withput passing the deviceid variable to get_a()
main.py:-

global deviceid
import lib
deviceid=123
lib.get_a()

lib.py:-

def get_a():
    global deviceid
    prnit deviceid  

calling main or trying to access deviceid from python shell is returning

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "t.py", line 4, in pp print a NameError: global name 'deviceid' is not defined

I tried giving global deviceid in many places inside and outside module and function.Nothing helps.

somebody please help.

Rilwan
  • 2,251
  • 2
  • 19
  • 28
  • Having looked here, http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them `deviceid` needs to be declared *outside* the two modules you appear to be using. Have you tried declaring it in the REPL before calling main? – avgvstvs Jul 17 '12 at 12:01
  • 3
    instead of defining globals, it seems to me that it would be much better if you just pass the variable as an argument, like this: `def get_a(deviceid)` – jonathan.hepp Jul 17 '12 at 12:04
  • In addition to the dex19dt's suggestion, which I fully support, you will be unable to import `main.py` from within `lib.py`, since you already import vice versa. Trying to do so would fail due to the circular dependency. – sloth Jul 17 '12 at 12:06
  • Passing argument will work.Then i wanted to edit the code in so many places.the example above showed is just part of a big file.Thanks. – Rilwan Jul 17 '12 at 12:06
  • Another way would be to add `deviceid` to a third file. – sloth Jul 17 '12 at 12:09
  • possible duplicate of [Global Variable from a different file Python](http://stackoverflow.com/questions/3400525/global-variable-from-a-different-file-python) – senderle Jul 17 '12 at 12:36

2 Answers2

2

There are no names in Python that are global to the entire program. Globals in Python are global to a particular module. To use a value in more than one module, you import its name from one module to another, making it available in two modules:

# main.py
import lib
deviceid = 123
lib.get_a()

# lib.py
import main
def get_a():
    print main.deviceid

As other commenters have pointed out, not using globals is probably a better option.

# main.py
import lib
deviceid = 123
lib.get_a(deviceid)

# lib.py
def get_a(deviceid):
    print deviceid
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • Your first code snippet is kind of dangerous. It will run as written, but if the 'global' was mutable, or was modified from `main.py` you would get surprising effects because the module `main` and the module executing the script from `main.py` are two different modules. – Duncan Jul 17 '12 at 13:45
  • I'm not sure what you mean by "surprising." If the value is meant to be shared, and is mutable, it is true, you need to be sure you want everyone to see the changes, because everyone will. – Ned Batchelder Jul 17 '12 at 13:50
  • 1
    Sorry, to be clear: if you run the script `main.py` that runs in the module `__main__`, but `lib.py` imports the module `main` so it gets a separate and different copy of any globals defined in `main.py`. For an immutable value that doesn't matter, but it will matter for lists or dicts or instances of user-defined classes. It may also matter for classes themselves. `surprising` for the opposite of what you said: *not everyone* will see the changes. – Duncan Jul 18 '12 at 07:33
0

You can put the deviceid variable in the lib module and use it via lib.deviceid

If you change the value of deviceid somewhere in your code by doing lib.deviceid = X, and if you reimport the lib module somewhere else and you request the deviceid again, it will have X as value.

nathan
  • 1,111
  • 3
  • 18
  • 33