[Python] Reuse Code in Multiple Projects with Python Modules

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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])))

 

以上是关于[Python] Reuse Code in Multiple Projects with Python Modules的主要内容,如果未能解决你的问题,请参考以下文章