品易云推流 关闭
文章详情页
文章 > Python高级 > python fire如何在函数和类中应用

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

头像

小妮浅浅

2021-06-07 09:25:434256浏览 · 0收藏 · 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电脑。

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图