【发布时间】:2016-03-27 07:45:54
【问题描述】:
我想从脚本而不是命令行运行这个函数。例如函数为:
def main():
parser = argparse.ArgumentParser(description='Caffe prototxt to mxnet model parameter converter.\
Note that only basic functions are implemented. You are welcomed to contribute to this file.')
parser.add_argument('caffe_prototxt', help='The prototxt file in Caffe format')
parser.add_argument('caffe_model', help='The binary model parameter file in Caffe format')
parser.add_argument('save_model_name', help='The name of the output model prefix')
args = parser.parse_args()
...
我怎样才能这样运行它?
file.main('file_1.csv', 'file_2.csv', 'name')
为什么有人会编写一个我只能从命令行运行的函数?感觉很不方便。
【问题讨论】:
-
写这个函数是因为在
if name == '__main__'下写代码通常不好。 -
嗨@wRAR,你有什么参考资料让我读一下为什么会这样?非常感谢。
-
很简单,当你需要从不同的地方调用它时,你不能像在单独的函数中编写的代码那样调用写在
if name == '__main__'下的代码。 -
啊。我的意思是为什么不写一个带参数的函数。例如。 def main(file1, file2, name):... @wRAR
-
您可能想从不同的脚本等中调用
main()。OTOH 将使用已解析参数的实际代码分离到单独的函数中是一个非常好的主意。
标签: python-2.7 argparse mxnet