码迷,mamicode.com
首页 > 编程语言 > 详细

[Python] Reuse Code in Multiple Projects with Python Modules

时间:2017-12-12 22:06:50      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:multiple   file   sys   return   project   modules   ide   ==   imp   

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

标签:multiple   file   sys   return   project   modules   ide   ==   imp   

原文地址:http://www.cnblogs.com/Answer1215/p/8028778.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!