python - 如何从一个文件中的函数获取数据到另一个文件
问题描述
我必须将一些值从一个文件导入另一个文件。我在 second.py 中有一个函数名 myFunc,其中我有一个变量名“a”。现在我想在 main 函数内的 first.py 文件中显示这个值。
first.py
-------
import os
import second.py import *
def main():
//here i need to print from another file
if __name__ == '__main__':
main()
second.py
---------
import os
def myFunc(var1, var2):
a = 'test user'
解决方案
以下是我在评论中建议的示例。
第一个.py
import os
from second import first_class
def main():
obj = first_class()
print(obj.a)
if __name__ == '__main__':
main()
第二个.py
import os
class first_class:
def __init__(self):
self.a = 'test user'
希望能帮助到你 !!
推荐阅读
- javascript - 警告:componentWillReceiveProps 已重命名,不推荐使用。请更新以下组件:控件
- javascript - 如何在用户操作之前停止执行 javascript 函数?
- node.js - ElectronJS 必须使用 import 来加载 ES Module
- javascript - 在 go 函数中等待 js 异步函数(promise)
- highslide - 如何禁用 highslide.js 中的所有动画?
- java - 如何为时间选择器设置默认时间?
- swagger - 如何在 OpenAPI/Swagger v3 中指定多部分表单字段是可选的?
- php - 身体 laravel 刀片中的头部渲染
- sql - 无法使用包含操作编写 SqlAlchemy 查询
- rust - 为什么当 NiTE 尝试从 OpenNI2 读取帧时会出现分段错误