PYTHON中的IMPORT以及__FUTURE__和ABSL的FLAGS使用

PYTHON中的IMPORT以及__FUTURE__和ABSL的FLAGS使用

zhuan:  https://www.freesion.com/article/742943624/

1.使用__FUTURE__模块 

from __future__ import absolute_import

from __future__ import division

from __future__ import print_function

Python 3.x引入了一些与Python 2不兼容的关键字和特性。在Python 2中,可以通过内置的__future__模块导入这些新内容。如果你希望在Python 2环境下写的代码也可以在Python 3.x中运行,那么建议使用__future__模块

1.1 from __future__ import absolute_import:

相对与绝对导入

1.2 from __future__ import division

精确除法

1.3 from __future__ import print_function

print用法

2.使用ABSL模块

 
from absl import app

from absl import flags

from absl import logging

程序中如何使用absl输入参数


import sys


from absl import app

from absl import flags

from absl import logging


#设置参数,第一个是参数名称,第二个是参数默认值,无默认值可取None,第三个是参数解释

flags.DEFINE_string('str_1', 'hello',

'Input a string.')

flags.DEFINE_string('str_2', 'world',

'Input a string.')


flags.DEFINE_integer('num_1', 0,

'Input a integer.')

flags.DEFINE_integer('num_2', 0,

'Input a integer.')


FLAGS = flags.FLAGS


def main(argv=()):

del argv

#打印时间,以及Python版本号

version = sys.version_info

logging.info('Running under Python {0[0]}.{0[1]}.{0[2]}'.format(version))


str3 = FLAGS.str_1 + FLAGS.str_2 #计算输入两个字符串的和-拼接字符串

print(str3)

c = FLAGS.num_1 * FLAGS.num_2 #计算输入两个整数的积

print(c)


# 如果当前是从其它模块调用的该模块程序,则不会运行main函数!

# 而如果就是直接运行的该模块程序,则会运行main函数。

if __name__ == '__main__':

flags.mark_flag_as_required('str_1')

flags.mark_flag_as_required('str_2')

flags.mark_flag_as_required('num_1')

flags.mark_flag_as_required('num_2')

# 执行程序中main函数,并解析命令行参数!

app.run(main)

打开终端或者Windows的命令提示符,输入python 文件名 -help就可以查看文件参数信息,例如:

53e065b7ab6b944ab8d6704132f5fa7b.png

 

输入参数方式:

163d986e2bd54db86d0e11dd1a39531f.png

 

 但是一直都有提示很烦:

 
F:\Anaconda3\envs\tensorflow-gpu\lib\site-packages\absl\flags\_validators.py:359: UserWarning: Flag --str_1 has a non-None default value; therefore, mark_flag_as_required will pass even if flag is not specified in the command line!

'command line!' % flag_name)

F:\Anaconda3\envs\tensorflow-gpu\lib\site-packages\absl\flags\_validators.py:359: UserWarning: Flag --str_2 has a non-None default value; therefore, mark_flag_as_required will pass even if flag is not specified in the command line!

'command line!' % flag_name)

F:\Anaconda3\envs\tensorflow-gpu\lib\site-packages\absl\flags\_validators.py:359: UserWarning: Flag --num_1 has a non-None default value; therefore, mark_flag_as_required will pass even if flag is not specified in the command line!

'command line!' % flag_name)

F:\Anaconda3\envs\tensorflow-gpu\lib\site-packages\absl\flags\_validators.py:359: UserWarning: Flag --num_2 has a non-None default value; therefore, mark_flag_as_required will pass even if flag is not specified in the command line!

'command line!' % flag_name)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值