ITPub博客

首页 > IT职业 > IT图书 > python fire如何在函数和类中应用

python fire如何在函数和类中应用

IT图书 作者:greatleaf 时间:2021-09-11 09:57:23 0 删除 编辑

1、使用函数

fire可非常简单快速地实现命令功能。

$ python hello.py
Hello World!
$ python hello.py --name=Prodesire
Hello Prodesire!
$ python hello.py --help
INFO: Showing help with the command 'hello.py -- --help'.
 
NAME
    hello.py
 
SYNOPSIS
    hello.py <flags>
 
FLAGS
    --name=NAME

2、使用类

使用函数是最简单的方式,如果想以更有组织的方式来实现,比如使用类,fire 也是支持的。

import fire
 
class Calculator(object):
  """A simple calculator class."""
 
  def double(self, number):
    return 2 * number
 
  def triple(self, number):
    return 3 * number
 
if __name__ == '__main__':
  fire.Fire(Calculator)

以上就是python fire在函数和类中的应用,希望对大家有所帮助。更多Python高级指路:python高级

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4236/viewspace-2830294/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-12

  • 博文量
    76
  • 访问量
    610