Class 继承文件库argparser中的命令

Class 继承文件库argparser中的命令,class,python-3.x,Class,Python 3.x,下午好 我想知道如何从使用lib argparser创建命令的文件中继承命令。 为了便于理解,我将举例说明: 文件1:(项目房屋可能没有任何无用的东西) 导入文件2 代码在这里 文件2:(包含argparser值) 导入argparse 类文件2(): def函数(): argumentos=argparse.ArgumentParser(描述=‘我的程序’) argumentos.add_参数('-command') argumentos_recebe=argumentos.parse_arg

下午好
我想知道如何从使用lib argparser创建命令的文件中继承命令。
为了便于理解,我将举例说明:
文件1:(项目房屋可能没有任何无用的东西)

导入文件2 代码在这里 文件2:(包含argparser值)

导入argparse 类文件2(): def函数(): argumentos=argparse.ArgumentParser(描述=‘我的程序’) argumentos.add_参数('-command') argumentos_recebe=argumentos.parse_args() 我想invez调用文件2我使用两个命令之一调用了该文件。
示例:
arquivo1.py-命令
而不是..
arquivo2.py-命令

我认为这是可能的使用类最喜欢找出如何

file1.py
导入
file1
时,可以将其内容作为
file1
模块对象上的属性引用。因此,您的
file1
代码可以是:

import file2

object = file2.file2()  # create an instance of the class
object.function()       # call the function to parse the arguments

现在,这与您显示的
file2
内容的编写方式不太一样,因为您的
function
方法不接受
self
参数。实际上,您可能不需要类(与Java不同,您不需要在Python中一直将所有函数打包到类中)。如果是这种情况,您可以将
函数
移动到模块的顶层,并删除“我的代码”的
对象
部分。然后只需调用
file2.function()
,它就会进行解析。请注意,如果需要在
文件1

中访问其余代码中的结果,您可能希望从正在进行解析的函数中返回一些内容!请告诉我更多的疑问,我如何将命令file2中传递的值拉到file1?通过文件返回2是否有效?键入return(parser.parse\u args.parser\u argument())是否有效?我实际上不太清楚
argparse
,但一般来说,您可以从
file2
中的函数返回您想要的任何内容,并在调用该函数后在
file1
中使用该返回值。 import argparse class file2(): def function(): argumentos = argparse.ArgumentParser(description = 'My Program') argumentos.add_argument('-command') argumentos_recebe = argumentos.parse_args()
import file2

object = file2.file2()  # create an instance of the class
object.function()       # call the function to parse the arguments