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

时间:2019-11-22 00:12:10

标签: function class get attributes var

我有2个模块,第一个模块具有类One,其类具有返回值的函数。第二个类具有2个具有函数的类TwoThree。我已经将类从第一个模块导入到第二个模块中。

在类i的函数Two中,我已将函数x从类One分配给y。从那里,我可以通过打印y来访问返回的值,该函数还返回程序中其他位置需要的变量type

,但还需要从类y的函数z中访问相同的变量Three

我在类Three中使用的方法返回错误。我尝试使用getattr,但没有发现任何乐趣。但是我相信我可能一直在用错它。

我仅有的其他解决方案是返回ytype。然后将类i中的函数Two分配给类pop的函数z中的Three。但这会从类x中的One中调用该函数,这意味着我必须输入另一个值,然后它会打印多行不需要的行。

我已经创建了这个问题的模型来尝试找到解决方案,但是我有些困惑。我需要在许多其他类中多次访问类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'

方法2:

模块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

1 个答案:

答案 0 :(得分:0)

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

Two类中,我不再需要将One.x(self)分配给y。 现在可以从模块“ TestOne.py”中导入“ my_dict ['Go']”的值

模块: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