如何从另一个 python 文件 运行 运行

How to run function from another python file

我有 a.py :

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

通常这会得到 运行 为:按 F5 & 在 shell window 中,给出命令为:

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

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

         For I in range 2:

现在我要求,如果 I==0,state.hello 必须执行并打印 happy。

与 I==1 类似,应该执行 state.bye 并打印 Sad。

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

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

from fileA import boxfinboxfinitestatemachine

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