一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

在python中实现导入一个需要传参的模块代码示例

时间:2021-05-12 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下在python中实现导入一个需要传参的模块代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

举个例子:

文件test1.py

import argparse
from argparse import RawTextHelpFormatter
              
parse = argparse.ArgumentParser(description="The parameters for the feature select method", formatter_class=RawTextHelpFormatter)
parse.add_argument('name', type = str)
 
args = parse.parse_args() 
name = args.name

文件test3.py

import test1 
str = test1.name + 'asdasdasd'

文件test2.py

import test3 
print test3.str

运行:

我们可以通过改变命令行的参数来改变这个name的值,其实关于这一点也比较好理解,我们可以想成python将这三份代码拼接在一起,再一起执行。

热门栏目