function - 如何从另一个模块中的函数(方法)中访问变量
问题描述
我有 2 个模块,第一个模块有One
一个带有返回值的函数的类。第二个有 2 个类Two
和Three
功能。我已将第一个模块中的类导入到第二个模块中。
在i
Class 的函数中,Two
我将函数x
从 class分配One
给y
. 从那里我可以通过打印访问返回值y
,该函数还返回type
程序中其他地方所需的变量。
但还需要从类y
中的函数内访问相同的变量。z
Three
我在课堂上使用的方法Three
返回错误。我试过使用getattr
但没有发现快乐。但我相信我可能用错了。
我唯一的其他解决方案是y
与type
. 然后将 class 中的函数分配给classi
的函数中。但这会从类中调用该函数,这意味着我必须输入另一个值,然后它会打印多个不需要的行。Two
pop
z
Three
x
One
我已经创建了这个问题的模拟来尝试找到解决方案,但我有点卡住了。我需要在许多其他类中多次 访问类y
函数中的值。i
Two
方法一:
模块 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
解决方案
在完成研究并寻找类似问题后,我发现使用字典是能够访问所需信息的更简单、更安全的方法之一。在尝试使用 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
. '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
推荐阅读
- css - CSS规则应用于边缘的错误方向
- python - Eclipse/Python pandas.read-csv - OSError:从文件初始化失败
- postgresql - 如何在谷歌云平台中使用私有 IP 从我的电脑连接到 sql 实例
- javascript - 我手动触发的 Vuetify 扩展面板在您第一次触发时没有动画。首次激活后正常工作
- java - 从Java中的多个列表中获取所有重复值
- vue.js - Vue.js 3按钮在第一次点击后不起作用
- jquery - 如何响应式地在浏览器中显示相同大小的 X 图像
- css - 不能用我自己的 css 引导导航栏颜色
- css - 具有旋转线性渐变背景的填充屏幕
- python - Pandas 中的 Groupby 列并执行计算(Python)