36-Python的模块使用

每一个以py作为扩展名的文件都是一个模块。

star.py:

hi = 'hello world!'

def pstar(n=50):
print('*' * n)

if __name__ == '__main__':
pstar()
pstar(30)

在Python交互模式中调用star模块:

hejianping@ubuntu:~$ python 
Python 2.7.12 (default, Dec 4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import star
>>> print(star.hi)
hello world!
>>> star.pstar()
**************************************************
>>> star.pstar(30)
******************************
>>>

结果输出:

原文地址:https://www.cnblogs.com/hejianping/p/10881235.html