如何使用python file.py从命令行运行类?

use*_*836 1 python linux terminal class

当我运行python filename.py时,它不会提示输入或打印输出.运行类Base1()需要运行什么命令?

class Base(TestCase):

    def setUp(self):
       #prompts for inpt
       ......

class Base1(Base):

    def base1(self):
        print('.......')
        return x

    def base2(self): 
        output = Base1.base1(self)
        print(output)
Run Code Online (Sandbox Code Playgroud)

Tim*_*rce 5

您的程序必须包含在程序在命令行上运行时将执行的顶级语句(即不缩进).

class Base(TestCase):

    def setUp(self):
       #prompts for inpt
       ......

class Base1(Base):

    def base1(self):
        print('.......')
        return x

    def base2(self): 
        output = Base1.base1(self)
        print(output)

# These commands will be executed when "python filename.py" is run from a shell
foo = Base1()
foo.base1()
....
Run Code Online (Sandbox Code Playgroud)


小智 5

qwrrty建议使用什么,但我建议把它放在一个主函数中,在python中完成

def main():
    foo = Base1()
    foo.base1()

if __name__ == "__main__":
    main()
Run Code Online (Sandbox Code Playgroud)