zoukankan      html  css  js  c++  java
  • [Python] Reuse Code in Multiple Projects with Python Modules

    A module is a function extracted to a file. This allows you to import the function and use it in any other code you may write. You’ll learn how to create modules, import them, and make them stand-alone as you learn what if __name__ == “__main__” means in Python.

    If we excute the file in REPL, __bname__ is __main__, if we import the file as module, __name__ is file name.

    def total(n):
        tax_rate = .07
        return n * tax_rate + n
    
    def tax_amount(n):
        tax_rate = .07
        return n * tax_rate
    
    # Provide a standalone way for user to use with CMD
    # python3 tax.py 10
    if __name__ == "__main__":
        import sys
        print(total(int(sys.argv[1])))
  • 相关阅读:
    using 关键字在 C++ 中的几种用法
    Chromium 修改图片资源
    SAM&广义SAM
    Burnside和Polya
    笔记:杜教筛
    笔记:莫比乌斯反演
    Miller-Rabin
    点分治
    虚树
    计算几何
  • 原文地址:https://www.cnblogs.com/Answer1215/p/8028778.html
Copyright © 2011-2022 走看看