python - Python - 无法从类中的 py 文件调用函数?
问题描述
我试图Myfunction
从 py 脚本调用,这发生在一个类中。但我收到一个错误,说Myfunction
未定义。我该如何避免这种情况?我知道我可以将所有内容file_that_contains_Myfunction_versionA(B).py
放入一个类中并调用它main()
。但是还有其他方法吗?
这里的诀窍是函数的名称是相同的,我不能在一开始就导入脚本,因为我不知道条件,直到我运行Myclass()
。
class Myclass():
def __init__(self):
self.main()
def main(self):
if conditionA:
exec(open("./file_that_contains_Myfunction_versionA.py").read())
Myfunction()
if conditionB:
exec(open("./file_that_contains_Myfunction_versionB.py").read())
Myfunction()
解决方案
您是否将另一个脚本导入到您从中调用它的脚本中?
from otherscript import function_A
function_A()
推荐阅读
- javascript - 这个内联 onClick 代码怎么能用 addEventListner 代替?
- android - 在 Java 11 上编译 Android 数据绑定的 JAXB 依赖项放在哪里?
- powershell - 如何将字符串中的每个字符更改为字母表中的前一个字符
- http - http1.1 Keep-Alive 没有流水线
- php - 使用AJAX插入数据但Datatable显示没有结果
- asp.net-web-api - C# .NET Core 2.2 Web-Api 500 仅在 Kestrel 上仅一个端点
- spring-boot - 使用 SseEmitter 时媒体类型不可接受的异常
- html - 如何正确重置网格模板列?
- javascript - JS 倒计时 5pm 前下单,其他 3pm 产品发出
- sql - 我对此查询的分组有问题