如何从另一个模块中的函数(方法)中访问变量

How can I access a Variable from within a Fuction (Method) that's in another Module

我有 2 个模块,第一个模块有 class One 和一个 return 值的函数。第二个有 2 classes TwoThree 函数。我已经将第一个模块中的 class 导入到第二个模块中。

在 Class Two 的函数 i 中,我已将函数 x 从 class One 分配给 y.从那里我可以通过打印 y 来访问 returned 值,该函数还 returns 一个变量 type 在程序的其他地方需要。

但还需要从函数 z in class Three.

中访问同一个变量 y

我在classThreereturn中使用的方法出错了。我试过使用 getattr 但没有发现任何乐趣。但我相信我可能用错了。

我唯一的其他解决方案是 return ytype。然后将classTwo中的函数i赋值给classThree的函数z中的pop。但这会调用 x in class One 中的函数,这意味着我必须输入另一个值,然后它会打印多个不需要的行。

我已经创建了这个问题的模拟来尝试找到解决方案,但我有点卡住了。我需要在 class Two 的函数 i 中访问 y 中的值,并在许多其他 class 中多次访问。

方法一:

模块 1:TestOne.py

class One():
    def x(self):
        Go = input("Please enter value\n")
        return Go


模块 2:Test.py

from TestOne import*


class Two():
    def i(self):
        type = "Move"
        y = One.x(self)
        print("Test 1: ",y)
        return  type




class Three():
    def z(self):
        print("Test 2: ", Two.i.y)

模块 3:TestMain.py

from Test import*


p = Two()

t =Three()

p.i()
t.z()

错误:

PS C:\Userscom\Python> python testmain.py
Please enter value

Test 1:
Traceback (most recent call last):
  File "testmain.py", line 9, in <module>

    t.z()
  File "C:\Userscom\Python\Test.py", line 16, in z
    print("Test 2: ", Two.i.y)
AttributeError: 'function' object has no attribute 'y'

方法二:

模块 1:TestOne.py

class One():
    def x(self):
        Go = input("Please enter value\n")
        return Go

模块 2:Test.py

from TestOne import*


class Two():
    def i(self):
        type = "Move"
        y = One.x(self)
        print("Test 1: ",y)
        return  type, y




class Three():
    def z(self):
        pop = Two.i(self)[1]
        print("Test 2: ", pop)

模块 3:TestMain.py:

from Test import*


p = Two()

t =Three()

p.i()
t.z()

输出:

PS C:\Userscom\Python> python testmain.py
Please enter value
1
Test 1:  1

Please enter value
1
Test 1:  1
Test 2:  1

编辑: 我做了一些挖掘并找到了解决问题的解决方案。使用 global。但是发现很多文章都说global如果使用不当会有些危险,

方法 3:工作解决方案。满足期望的输出。

模块 1:TestOne.py

class One():
    def x(self):
        Go = input("Please enter value\n")
        return Go

模块 2:Test.py

from TestOne import*

class Two():
    def i(self):
        type = "Move"
        global y
        y = One.x(self)
        print("Test 1: ",y)
        return  type


class Three():
    def z(self):

        print("Test 2: ", y)

模块 3:TestMain.py:

from Test import*


p = Two()

t =Three()

p.i()
t.z()

输出:(期望的输出)

PS C:\Userscom\Python> python testmain.py

Please enter value
1
Test 1:  1
Test 2:  1

经过研究和寻找类似问题后,我发现使用字典是能够访问所需信息的更简单、更安全的方法之一。 在尝试使用 global 时,我发现我遇到了很多问题,而且程序也没有像预期的那样 运行。使用当前方法,我可以通过将它们导入所需的模块来访问存储在字典中的值,并且还可以轻松地进行更改

这是修改后的解决方案:

模块 1:TestOne.py

my_dict ={'Go':None}

class One():
    def x(self):
        my_dict['Go'] = input("Please enter value\n")

my_dict 定义为具有键的字典:Go,值设置为 None。 在 class One 的函数 x 中,my_dict['Go'] 被赋予从 input.

派生的值

模块 2:Test.py

from TestOne import*


class Two():
    def i(self):
        type = "Move"

        One.x(self)
        print("Test 1: ", my_dict["Go"])
        return  type




class Three():
    def z(self):

        print("Test 2: ", my_dict["Go"])

在 class Two 中,我不再需要将 One.x(self) 分配给 y。 'my_dict['Go']` 的值现在可以访问,因为它是从模块 'TestOne.py'

导入的

模块:TestMain.py

from Test import*


p = Two()

t =Three()

p.i()
t.z()

输出:

Please enter value
1
Test 1:  1
Test 2:  1

Accessing "Module Scope" Var