【问题标题】:How to run function from another python file如何从另一个python文件运行函数
【发布时间】:2020-09-01 18:31:15
【问题描述】:

我有一个.py:

   Class boxfinitestatemachine(models):
   ..... 
       @transistion...
       def hello(self):
           Print happy
       ....
       @transostion..
       def bye(self):
           Print sad
        ....

通常这会运行为:在 shell 窗口中按 F5 &,给出命令:

        >>>State= boxfinitestatemachine()      #input
        >>> State.hello() #input
        >>> happy         #output
        >>> State.bye()  # input
        >>> Sad           #output

现在我有了另一个文件 b.py,其中包含 FOR 循环函数:

         For I in range 2:

现在我需要,如果我==0,state.hello 必须被执行并且应该打印快乐。

同样,对于 I==1,应该执行 state.bye 并打印 Sad。

如何执行此要求。请提供任何帮助。

【问题讨论】:

  • 您能否说明您正在尝试做什么,您尝试做什么,以及您遇到了什么问题?您的描述看起来像是您想做if I == 0: State.hello(),您实际上可以这样做。你在找import吗?
  • 这能回答你的问题吗? Call a function from another file in Python
  • a.py 实际上做了有限状态机转换功能。如果我先按 state.sad 的顺序执行,将根据脚本中定义的转换状态抛出转换错误,并且在我的最终测试应用程序中也应该抛出错误......所以在另一个文件 b.py 中,我有排列list (0,1),(1,0).. 所以将调用第一个 list(0,1) 并检查流程..
  • 建议的链接没有帮助。其实我之前已经提到过。但是这里 a.py 是有限状态机转换.. 定义的函数将在终端中调用,如上所述。

标签: python function fsm


【解决方案1】:

您可以在文件b上导入boxfinboxfinitestatemachine,并在终端上使用。

from fileA import boxfinboxfinitestatemachine

State = boxfinboxfinitestatemachine()
for i in range 2:
  if(i == 0):
    State.hello()
  if(i == 1):
    State.bye()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 2013-02-24
    • 2012-12-15
    • 1970-01-01
    相关资源
    最近更新 更多