是否可以在 python 中创建一个主脚本,在其中链接所有其他脚本?例如制作 ax 脚本和 y 脚本并将它们链接到主脚本中,因此当我运行主脚本时,代码将成功运行。
是的,如果主文件的代码在函数内,则有一种方法。
假设主脚本 ( file1.py
):
def main():
return "Hello world!"
if __name__ == "__main__":
print(main())
然后在第二个脚本中,您可以导入和调用函数,如:
import file1
print(file1.main())
让我们更进一步。
这里我们在主脚本中有一个类:
class myClass:
def __init__(self):
self.x = 10
def getHello(self):
return "Hello world!"
现在我们可以使用主脚本中的类,例如:
import file1
theClass = file1.myClass()
print(theClass.x)
print(theClass.getHello())
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句