如何在python中通过命令行从类中调用特定函数

2024-10-01 15:44:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python文件test1.py,它有以下代码(例如)-

class Testing(BaseTestRemote):

    def collect_logs(self):

    def delete_logs(self):

那么如何从命令行运行collect_logs()(在类测试中存在),您能举个例子吗?你知道吗


Tags: 文件代码命令行pyselfdefdeletetesting
1条回答
网友
1楼 · 发布于 2024-10-01 15:44:31

让我们创建一个名为file.py的文件,其内容如下:

class test(object):

    def __init__(self):
        return

    def square(self, x):
        return x*x

    def cube(self, x):
        return x*x*x

从包含file.py的目录运行命令行并执行以下操作:

~$ python
Python 2.7.11+ (default, Apr 17 2016, 14:00:29) 
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import file
>>> obj = file.test()
>>> obj.square(2)
4
>>> obj.cube(4)
64
>>> from file import test
>>> test().square(2)
4
>>> test().square(4)
16
>>> x = test()
>>> x.square(2)
4
>>> x.square(4)
16
>>> 

相关问题 更多 >

    热门问题