Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中的文件之间共享变量:为什么每次文件使用变量时都会修改变量?_Python_Variables - Fatal编程技术网

在python中的文件之间共享变量:为什么每次文件使用变量时都会修改变量?

在python中的文件之间共享变量:为什么每次文件使用变量时都会修改变量?,python,variables,Python,Variables,[编辑]我将testons_常量重命名为testons_变量,以更清楚地表明我希望能够修改变量。如果注释中出现testons_常量,请考虑它对应于新名称testons_变量 我试图理解python中文件之间如何共享变量 为此,我制作了三份文件: testons.py: import testons_variables import testons_functions # The goal of this file is to understand how python deals with v

[编辑]我将testons_常量重命名为testons_变量,以更清楚地表明我希望能够修改变量。如果注释中出现testons_常量,请考虑它对应于新名称testons_变量

我试图理解python中文件之间如何共享变量

为此,我制作了三份文件:

testons.py:

import testons_variables
import testons_functions

# The goal of this file is to understand how python deals with variables shared between files 



print(testons_variables.A) # We can access the variable from the other file
testons_variables.A=5 # We can modify its value
print(testons_variables.A)
testons_functions.fct()
print(testons_variables.A) # The value has been modified from testons_functions
A=0
testons\u variables.py:

import testons_variables
import testons_functions

# The goal of this file is to understand how python deals with variables shared between files 



print(testons_variables.A) # We can access the variable from the other file
testons_variables.A=5 # We can modify its value
print(testons_variables.A)
testons_functions.fct()
print(testons_variables.A) # The value has been modified from testons_functions
A=0
testons\u functions.py

import testons_variables

def fct():
    print(testons_variables.A) # this print will show the value of the variable from testons before the call of the function, and not from
    # testons_variables
    testons_variables.A=50 # We can modify the value
    print(testons_variables.A) 
以下是我运行testons.py时的输出:

0
5
5
50
50
现在,通过“反向理解”,我意识到无论在哪里修改变量testons_variables.A,都会对使用它的所有文件进行修改。实际上,如果我在文件testons.py中修改它,它也会在testons_functions.py中修改。如果我在testons_functions.py中修改它,它也会被修改为testons.py

这是我现在的理解。因此,似乎文件之间共享的变量对每个人都进行了修改,无论修改发生在哪里

让我困惑的是,如果我们使用全局变量,要在函数中修改它们,我们需要关键字“global”来允许全局修改。我从来没有在这里用过这样的关键词。我知道情况并不完全相同(我没有在一个唯一的文件中使用函数内部的全局变量),但我对这种行为感到不安,这让我觉得我可能遗漏了一点


我想知道我是否正确理解在文件之间共享变量时会发生什么,这是我的问题。只需在
teston.py
teston\u functions.py
中打印导入的
teston\u变量的引用即可

print(id(testons_variables))
您会注意到,两个print语句都提供相同的值,因此,尽管它们在不同的位置导入,但它们使用相同的引用

这意味着一个内存中的更改将影响其他内存,因为它们指向相同的内存位置


现在,关于
global
关键字的用法,它只在您需要更改变量的值并且没有范围时才有用。

模块也是对象。在函数范围之外的模块中定义变量时,该变量属于该模块。它不是全球性的


当您导入一个模块时,您得到的模块对象与其他任何导入该对象的人得到的模块对象相同。Python缓存模块,因此无论您导入模块多少次或导入多少个位置,始终只会得到一个包含该模块的对象。因为模块是共享的,所以该模块中的所有变量都是共享的。

将变量作为参数发送到函数并返回值通常被认为是更好的做法。这将避免混乱和矛盾。此外,还可以使用
enum
存储常量@StarBucK@Vishnudev对于我目前的项目来说,这样做将是一场噩梦,因为我有许多模拟testons_函数的函数(大约200个)。必须明确地向它们发送与参数相同的变量,这意味着我的代码中存在大量不必要的重复。现在,也许还有其他更好的方式继续下去,我很有兴趣听到他们。尽管如此,我还是想检查一下我对这个问题的理解,即使这是编写代码的“糟糕方式=)对象(如模块)的变异对任何引用该对象的人都是可见的。为对象分配名称是本地的(更改名称的本地
\uuuu dict\uuuu
),除非使用
非本地
全局
来指示所涉及的命名空间。否。它不会重复,而是使用一个类及其可以被函数访问的变量来正确地构造代码。如果您采用全局共享变量方法,那将是一场调试噩梦。看看这个。这会让你知道为什么会这样。