首页 > 解决方案 > 如何从另一个模块中的函数(方法)中访问变量

问题描述

我有 2 个模块,第一个模块有One一个带有返回值的函数的类。第二个有 2 个类TwoThree功能。我已将第一个模块中的类导入到第二个模块中。

iClass 的函数中,Two我将函数x从 class分配Oney. 从那里我可以通过打印访问返回值y,该函数还返回type程序中其他地方所需的变量。

但还需要从类y中的函数内访问相同的变量。zThree

我在课堂上使用的方法Three返回错误。我试过使用getattr但没有发现快乐。但我相信我可能用错了。

我唯一的其他解决方案是ytype. 然后将 class 中的函数分配给classi的函数中。但这会从类中调用该函数,这意味着我必须输入另一个值,然后它会打印多个不需要的行。TwopopzThreexOne

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

方法一:

模块 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:\Users\3com\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:\Users\3com\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:\Users\3com\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:\Users\3com\Python> python testmain.py

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

标签: functionclassgetattributesvar

解决方案


在完成研究并寻找类似问题后,我发现使用字典是能够访问所需信息的更简单、更安全的方法之一。在尝试使用 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. x在类的函数中Onemy_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"])

在课堂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

访问“模块范围”变量


推荐阅读