• 技术文章 >Python技术 >Python高级

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

    小妮浅浅小妮浅浅2021-06-07 09:25:43原创4049

    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电脑。

    专题推荐:python fire
    品易云
    上一篇:python中unittest的核心组成 下一篇:python部署虚拟环境的流程

    相关文章推荐

    • python Package如何设置文件入口• python如何实现条件选择• python有几种循环遍历的方法?• python整数和浮点数的常见用法• python单下划线是什么意思• python fire是什么

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网