Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.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 - Fatal编程技术网

Python中跨文件的值可访问性

Python中跨文件的值可访问性,python,Python,我的文件结构如下 Folder1: Folder2 Class-with-global-variables-and-static-methods-to-modify-them.py Folder2: Myfile-1.py Myfile-2.py Master.py Master.py导入其文件夹中的其他2个文件。“带有全局变量和修改它们的静态方法的类.py”从未实例化,其中的全局值通过静态方法修改。但我希望这些修改在全球范围内持续下去 例: 如果Myfile-1.py修改了重要性值,我希望

我的文件结构如下

Folder1:
Folder2
Class-with-global-variables-and-static-methods-to-modify-them.py

Folder2:
Myfile-1.py
Myfile-2.py
Master.py
Master.py导入其文件夹中的其他2个文件。“带有全局变量和修改它们的静态方法的类.py”从未实例化,其中的全局值通过静态方法修改。但我希望这些修改在全球范围内持续下去

例:


如果Myfile-1.py修改了重要性值,我希望Myfile-2.py在尝试访问它时可以使用新的重要性值。

我认为您只需要在var之前附加全局值,一切都应该正常

class Class-with-global-variables-and-static-methods-to-modify-them:
...
 global importance
 importance = 0
 ...
 ...
 @staticmethod
 def modifyImportance(value):
      Class-with-global-variables-and-static-methods-to-modify-them.imortance = 1

你已经拥有的东西会有用,但你做得不对


在Python中,拥有一个从不实例化且只包含静态变量的类是没有意义的。Python不是Java,不需要类。相反,只需在模块级别定义变量并直接修改它们

Myfile-1
Myfile-2
模块是否在同一执行上下文中执行/调用,或者您是否试图在不同执行之间保留状态?相同的执行上下文。Ideadlly Master.py可以调用Myfile-1和Myfile-2,但不必以预定义的顺序调用,我如何在模块级别定义它们?两个模块如何共享这些值?这将使它可以跨文件访问?不。我想我误解了这个问题(
class Class-with-global-variables-and-static-methods-to-modify-them:
...
 global importance
 importance = 0
 ...
 ...
 @staticmethod
 def modifyImportance(value):
      Class-with-global-variables-and-static-methods-to-modify-them.imortance = 1