简体   繁体   English

跨文件Python更改变量

[英]Changing variables across files Python

Heyo, I've run upon a problem. Heyo,我遇到了一个问题。 So I have three files: 所以我有三个文件:

  1. Main Program 主要计划
  2. Functions 功能
  3. Data 数据

And I want the Main Program to call a function from the Functions module, which changes a variable in Data. 我希望主程序从Functions模块调用一个函数,它可以更改Data中的变量。 Then I need to use the new variable elsewhere in the Main Program. 然后我需要在主程序的其他地方使用新变量。

This is what I want, shown as a simplified demonstration program: 这就是我想要的,显示为简化的演示程序:

The Data file: 数据文件:

#data.py
#just an short depiction of my actual file
text = ""

The Functions file: 函数文件:

#functions.py
from data import *

def printHi():
    global text
    text = "hi"
    print(text)

The Main Program: 主程序:

#mainProgram.py
from functions import *
from data import *

printHi()
print(text)

What I expected would happen would be that when I run the Main Program: 我预期会发生的事情是当我运行主程序时:

The Functions file and Data file is imported. 导入函数文件和数据文件。

Then it calls the "printHi" method from the Functions file. 然后它从Functions文件中调用“printHi”方法。

The variable "text" from the Data file is assigned "hi", and is printed. 数据文件中的变量“text”被指定为“hi”,并被打印。

The the Main Program prints the "text" variable. 主程序打印“文本”变量。

And I supposed that the text variable would be "hi". 我认为文本变量将是“hi”。 However, to my disappointment, it prints blank. 然而,令我失望的是,它打印为空白。 It does indeed print the initial text value. 它确实打印了初始文本值。

I really have NO idea why this is so. 我真的不知道为什么会这样。 Shouldn't the text variable have already been changed? 文本变量是否应该已经更改? Could you please explain what part about my program is wrong and how to correct it? 你能解释一下我的程序有哪些错误以及如何纠正?

The short answer is simply not to do this. 简短的回答就是不要这样做。 It's a Bad Idea for all the reasons that global variables are always bad ideas: because they lead to stack overflow questions that read like "If I do this thing I shouldn't do, it does something I didn't expect -- why did it do that?" 对于全局变量总是坏主意的所有原因,这是一个坏主意:因为它们导致堆栈溢出问题,如“如果我做这件事我不应该做,它做了我没想到的事情 - 为什么呢?它那样做?“ Whose easiest answer is, as you've now read, "That's why you shouldn't do that thing." 正如你现在所读到的那样,这是最简单的答案,“这就是你不应该做那件事的原因。”

The long answer is a bit beyond me without spending a whole lot more time on the matter, but the long er answer is simple enough. 长的答案是有点超出我没有对此事花了一大堆更多的时间,但龙儿答案是很简单的。 When you do those "star" imports ( from modulename import * ) you're rebinding the name of the variable. 当您执行那些“星形”导入( from modulename import * )时,您将重新绑定变量的名称。 What functions.printHi thinks of as text is not data.text but actually functions.text . functions.printHi data.text认为text不是data.text ,实际上是functions.text When it's changed in printHi , it changes functions.text which should still be okay, since mainProgram is also importing functions . 当它在printHi改变时,它会改变functions.text ,它应该仍然可以,因为mainProgram也是导入functions

However remember that mainProgram ISN'T actually importing functions , it's from functions import * 'ing. 但请记住, mainProgram实际上并不是导入functions ,而是from functions import * 'ing。 That means what mainProgram thinks of as text is neither data.text nor functions.text , but mainProgram.text . 这意味着mainProgram认为text既不是data.text也不是functions.text ,而是mainProgram.text When functions.printHi changes functions.text , it doesn't touch mainProgram.text . functions.printHi更改functions.text ,它不会触及mainProgram.text

The short answer applies here because these sorts of pitfalls are non-obvious unless you can think deeply enough about your code to understand them. 简短的回答适用于此,因为除非您能够深入思考您的代码以理解它们,否则这些陷阱是不明显的。 If you are able to think that deeply about your code, you should be able to write something that can sidestep such pitfalls entirely. 如果你能够深入思考你的代码,你应该能够写出一些可以完全避开这些陷阱的东西。 For instance: "global mutable state" is generally a bad thing. 例如:“全球可变状态”通常是一件坏事。 Avoid it. 躲开它。

To just make this work, drop all your "star" imports. 要完成这项工作,请删除所有“明星”导入。 The following code works: 以下代码有效:

# functions.py
import data


def printHi():
    # plus! You don't need the `global` anymore.
    data.text = "hi"
    print(data.text)

# mainProgram.py
import functions
import data


functions.printHi()  # prints "hi" from inside functions.printHi
print(data.text)     # also prints "hi"

Cool, we have a lot of people saying "don't do this!". 很酷,我们有很多人说“不要这样做!”。 Well, what should you do then? 那么,那你该怎么办? The good way to do this is to pass the text variable into the function and out of it. 执行此操作的好方法是将text变量传递给函数并将其传递给函数。 Like so: 像这样:

The Data file: 数据文件:

#data.py
#just an short depiction of my actual file
text = ""

The Functions file: 函数文件:

#functions.py
from data import *

def printHi(atext):
    atext = "hi"
    print(atext)
    return atext

The Main Program: 主程序:

#mainProgram.py
from functions import *
from data import *

text = printHi(text)
print(text)

That solves your problem. 这解决了你的问题。 You should probably also get rid of the * imports as the other answer suggests but that's a philosophical question. 你可能也应该像其他答案所暗示的那样摆脱*进口,但这是一个哲学问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM