1

This is my code

__Main_menu__.py:

def main_menu():
    global action
    action = input("Welcome where do you want to go? > ")

game.py:

from __Main_menu__ import *
main_menu()
print(action)

I was wondering how do you have it so that the action variable from __Main_menu__.py will be able to be reconised in game.py.

When I run game.py I get this error:

Traceback (most recent call last):
  File "/Users/gbrown/PycharmProjects/untitled/game.py", line 3, in <module>
    print(action)
NameError: name 'action' is not defined
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Griffin Filmz
  • 101
  • 1
  • 2
  • 5
  • 6
    Why not return it explicitly from `main_menu`? Then it's just `action = main_menu()` to access it in `game.py`. – jonrsharpe Oct 30 '15 at 12:30
  • 2
    Simply assign the variable outside of main_menu. The *global* indicator makes no global variable on its own, it would simply re-use a global variable if it exists instead of creating a new local one. – guidot Oct 30 '15 at 12:34

1 Answers1

0

Globals in Python are global to a module, not across all modules. Globals in python is not C global. So, when you try to import *, you create a new action global in your game.py module, but it's not the same from Main_menu.py, it's a new one. However, action continue to exist in Main_menu.py, and main_menu() function act on it.

A better way is to return explictly your variable, but, not the answer to your question. So, for you, do what you want, you must import explicitly what you want, and work with the module name to extract the real action global from Main_Menu.py:

__Main_menu__.py:

action = None
def main_menu():
   global action
   action = input("Welcome where do you want to go? > ")

Here, action is attached to the module/file Main_Menu.py. When you try to import *, you create in the context of the new module where you import, a new global action with None as value. But there is no link with the action that is define in the module, and where the function menu update the value.

If you import it in a more "classical" way, and keep the path (import Main_menu) or define a specific path (import _Main_menu__ as mm), in this case, there is a difference, there is no overlap between local global and other module global. So, in this case, it's possible to gain access to the global from the module, by refering the path.

game.py:

import __Main_menu__ as mm
mm.main_menu()
print(mm.action)

In this case, global never collide, and you can access to mm.action

BUT

global for THIS case is not optimal or a good practice. It's better to have this:

__Main_menu__.py:

def main_menu():
   action = input("Welcome where do you want to go? > "
   return action

Here, we never use global. We just want the function give us an information. This is why we use the return keyword, which mean : Return the local value to who call me.

game.py:

from __Main_menu__ import *
action=main_menu()
print(action)

Here, when you call main_menu(), main_menu() return a value, which you affect to the action variable. And you have your information, without use of global. It's better, because there is no overlap, there is no ambiguity, and you can use it in a more generic way. P.S. Some more information here : Stackoverflow information about it :p

A.H
  • 1,007
  • 5
  • 15