如何把一个文件中的一个类中的函数的返回值传到另一个文件的函数中 python代码思路
假设有两个文件,分别为a.py和b.py,其中a.py中有一个类ClassA,其中有一个函数funcA返回一个值,需要将这个值传到b.py中的一个函数funcB中。
- 在a.py中,先导入b.py中的函数funcB:
from b import funcB
- 在ClassA中的funcA中返回需要传递的值:
class ClassA:
def funcA(self):
# some code here
return value_to_pass
- 在b.py中定义funcB,参数为传递的值:
def funcB(value_passed):
# some code here
- 在需要传递值的地方,调用funcA获取值并传递给funcB:
from a import ClassA
a = ClassA()
value_to_pass = a.funcA()
funcB(value_to_pass)
这样就可以将a.py中ClassA的funcA的返回值传递给b.py中的funcB了
原文地址: https://www.cveoy.top/t/topic/hgTe 著作权归作者所有。请勿转载和采集!