你如何在不同的文件中使用带有输入的变量? [英] how do you use variables with inputs on different files?

查看:54
本文介绍了你如何在不同的文件中使用带有输入的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

__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)

我想知道你如何拥有它,以便 __Main_menu__.py 中的动作变量能够在 game.py 中重新调整.

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.

当我运行 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

推荐答案

Python 中的全局变量对于一个模块是全局的,而不是跨所有模块.python 中的全局变量不是 C 全局变量.因此,当您尝试导入 * 时,您会在 game.py 模块中创建一个新的全局操作,但它与 Main_menu.py 中的不同,它是一个新的.但是,Main_menu.py 中仍然存在 action,并且 main_menu() 函数对其进行操作.

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.

更好的方法是明确返回您的变量,但不是您问题的答案.因此,对您来说,做您想做的事,您必须明确导入您想要的内容,并使用模块名称从 Main_Menu.py 中提取实际操作全局:

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? > ")

此处,操作附加到模块/文件 Main_Menu.py.当您尝试导入 * 时,您会在导入的新模块的上下文中创建一个新的全局操作,其值为 None.但是没有链接到模块中定义的动作,以及功能菜单更新值的位置.

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.

如果以更经典"的方式导入,并保留路径(import Main_menu)或定义特定路径(import _Main_menu__ as mm),在这种情况下是有区别的,本地全局和其他模块全局之间没有重叠.因此,在这种情况下,可以通过引用路径从模块访问全局.

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)

在这种情况下,全局不会发生冲突,并且可以访问mm.action

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

但是

全局对于这种情况不是最佳的或好的做法.最好有这个:

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

在这里,我们从不使用全局.我们只是想让函数给我们一个信息.这就是我们使用 return 关键字的原因,意思是:将本地值返回给呼叫我的人.

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)

在这里,当您调用 main_menu() 时, main_menu() 返回一个值,您将其影响到操作变量.而且您拥有自己的信息,无需使用全局.更好,因为没有重叠,没有歧义,您可以以更通用的方式使用它.附注此处提供更多信息:有关它的 Stackoverflow 信息:p

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

这篇关于你如何在不同的文件中使用带有输入的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆