0

I am creating my global variables within my Main.py and trying to update through a function located in my States.py

The targeted code in my Main.py looks like so:

#!/usr/bin/env python
#
import States #Import created states.py

global rad
States.Update_var()
leway = 2.00/rad

And my States.py looks like so:

def Update_var():
global rad
rad = 180.000001/3.1415926

But when I run the code the rad variable doesn't update to the new value and I receive an error:

NameError: global name 'rad' is not defined

I am relitively new to python and just finding my feet so any help would be great. Thanks

Community
  • 1
  • 1
Ben411916
  • 257
  • 2
  • 10
  • [Use of "global" keyword in python](http://stackoverflow.com/questions/4693120/use-of-global-keyword-in-python) might be useful – Adam Smith Jan 18 '16 at 20:26

3 Answers3

1

You dont need global variable to pass the value between modules just use the function's return value.

Main.py

#!/usr/bin/env python
#
import States #Import created states.py

rad = States.Update_var()
leway = 2.00/rad

States.py

def Update_var():
    return 180.000001/3.1415926
Assem
  • 11,574
  • 5
  • 59
  • 97
1

The global keyword is only used to indicate that inside a context that would normally be local, the variable you're referring to is the global one. You're using it correctly in States.py, but main.py is putting global rad at the module scope. That's always wrong.

In this case, you're importing States and trying to use its module-level variable rad. You should remove the global rad line in main.py (since it's not being used correctly) and refer to that as States.rad

# states.py

rad = 0  # unset

def update_var():
    global rad
    rad = 1  # set

 

# main.py

import states

states.rad  # 0
states.update_var()
states.rad  # 1
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
1

If you need to update global variable by external code, you have to pass dictionary of globals to that code:

main.py:

import States

States.Update_var(globals())


leway = 2.00/rad
print leway

States.py:

def Update_var(dct):
    dct["rad"] = 180.000001 / 3.1415926

Having those two files in a directory, run:

$ python main.py
0.0349065842505

In case, it would complain about problems to import the States module, make sure it can be imported, e.g. by $ export PYTHONPATH="."

Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98
  • So is the global() built in? Essentially making every variable within the Update_var function global – Ben411916 Jan 18 '16 at 20:35
  • @Ben411916 the `globals()` is built in. If you create those two files and run `python main.py`, it will print `0.0349065842505`. – Jan Vlcinsky Jan 18 '16 at 20:37
  • Quick question, can I update the values of rad from within my main.py? – Ben411916 Jan 18 '16 at 20:43
  • @Ben411916 Yes, you can. In top level (outside function) you can do that directly. From within functions you have to declare the variable in global scope you want to change by `global rad`. – Jan Vlcinsky Jan 18 '16 at 20:48