本章目標:Python代碼重用。將可重複使用的函數放到一個模塊中,並將模塊發佈出來,並放到Python軟件共享網站。python
1、Python 代碼註釋方法:linux
一、多行註釋,使用三重引號(單引號和雙引號均可以)。shell
二、單行註釋使用#符號。函數
2、建立和使用模塊,將函數寫入一個後綴名爲.py的文件中並保存,就作成了一個模塊,將模塊放入Python搜索路徑中就能夠經過import引入了,Python的搜索路徑是指哪裏呢?能夠在Python Shell 中輸入import sys;sys.path看到。一般相似如下內容:網站
['', 'D:\\Python35\\Lib\\idlelib', 'D:\\Python35\\python35.zip', 'D:\\Python35\\DLLs', 'D:\\Python35\\lib', 'D:\\Python35', 'D:\\Python35\\lib\\site-packages']ui
使用模塊時,先用import引用,而後再使用模塊中的函數,一般有如下兩種方法:url
一、只簡單的使用import引用。spa
import 模塊名 #引用模塊 模塊名.函數名() #調用模塊中的函數
在這裏,咱們能夠看出,只是簡單使用import引用時,調用模塊中的函數,須要在函數前寫上函數所在的模塊名。code
二、使用from 模塊名 import 函數名 方式引用。
orm
from 模塊名 import 函數名 #引用模塊並聲明引用函數 函數名() #調用模塊中的函數,再也不須要模塊名。
這種引用方式雖然能夠在調用函數時再也不須要寫模塊名,可是若是當前命名空間中已經有一個和這函數同名的函數,就會被引用的函數覆蓋掉。
3、發佈(distribution)模塊
一、爲模塊建立一個文件夾,並將模塊文件複製到文件夾中
二、在文件夾中建立文件setup.py,在文件中包含有關發佈的元數據。
#例子 from distutils.core import setup setup( name = 'lumNester', version = '1.0.0', py_modules = ['lumNester'], author = 'luming', author_email = 'luming@gmail.com', url = 'http://ww.test.com', description = 'A Test', )
三、構建一個發佈文件:在終端窗口中,使用命令python setup.py sdist 。
結果大概以下:
running sdist running check warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list) warning: sdist: standard file not found: should have one of README, README.txt writing manifest file 'MANIFEST' creating lumNester-1.0.0 making hard links in lumNester-1.0.0... hard linking lumNester.py -> lumNester-1.0.0 hard linking setup.py -> lumNester-1.0.0 creating dist creating 'dist\lumNester-1.0.0.zip' and adding 'lumNester-1.0.0' to it adding 'lumNester-1.0.0\lumNester.py' adding 'lumNester-1.0.0\PKG-INFO' adding 'lumNester-1.0.0\setup.py' removing 'lumNester-1.0.0' (and everything under it)
四、將發佈安裝到你的Python中。
在終端窗口中,使用命令 python setup.py install (若是是在linux中可能會須要sudo)。
結果大概相似以下內容:
running install running build running build_py creating build creating build\lib copying lumNester.py -> build\lib running install_lib copying build\lib\lumNester.py -> d:\Python35\Lib\site-packages byte-compiling d:\Python35\Lib\site-packages\lumNester.py to lumNester.cpython-35.pyc running install_egg_info Writing d:\Python35\Lib\site-packages\lumNester-1.0.0-py3.5.egg-info
五、將模塊上傳到PyPI。
(1)若是第一次使用PyPI,先到pypi.python.org網站註冊一個用戶。註冊好以後,在終端窗口中,使用命令python setup.py register。
running register running check We need to know who you are, so please choose either: 1. use your existing login, 2. register as a new user, 3. have the server generate a new password for you (and email it to you), or 4. quit Your selection [default 1]: 1 Username: #這裏輸入在網站註冊的用戶名密碼 Password: Registering lumNester to https://pypi.python.org/pypi Server response (200): OK I can store your PyPI login so future submissions will be faster. (the login will be stored in D:\emacs\emacshome\.pypirc) Save your login (y/N)?y #把用戶密碼信息保存,之後就不用再來這一步了。
(2)上傳模塊,使用命令python setup.py sdist upload。之後若是模塊作了修改,那麼要修改setup.py中version(即版本號),而後從新運行這個命令。
running sdist running check warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list) warning: sdist: standard file not found: should have one of README, README.txt writing manifest file 'MANIFEST' creating lumNester-1.0.0 making hard links in lumNester-1.0.0... hard linking lumNester.py -> lumNester-1.0.0 hard linking setup.py -> lumNester-1.0.0 creating 'dist\lumNester-1.0.0.zip' and adding 'lumNester-1.0.0' to it adding 'lumNester-1.0.0\lumNester.py' adding 'lumNester-1.0.0\PKG-INFO' adding 'lumNester-1.0.0\setup.py' removing 'lumNester-1.0.0' (and everything under it) running upload Submitting dist\lumNester-1.0.0.zip to https://pypi.python.org/pypi Server response (200): OK
六、使用pip安裝本身的模塊(前提是你的模塊已經上傳到PyPI了),在終端窗口中,使用命令 pip install 模塊名。
七、若是網站上的模塊更新之後,能夠用命令pip install -U 模塊名 來升級模塊。-U也能夠用--upgrade代替。
4、已發佈模塊修改原則
一、已經發布的模塊中函數若是須要增長參數時,應該使用帶默認值的參數,避免用戶更新模塊後沒法使用。
例如:原函數def print_lol(the_list) 須要增長一個參數level,能夠這樣修改
def print_lol(the_list,level=0)
這裏level=0就是給參數一個默認值,那麼已經使用這個函數的程序不須要修改就可使用新函數。
二、函數修改後,在使用默認參數時,獲得的結果應該和原函數保持一致,避免已經使用該函數的程序產生問題。
5、新內置函數及使用
print的新用法:print(varname,end='') 使用參數end='',能夠輸出不帶回車換行的字符。
製表符能夠用'\t'表示。
range(n),生成迭代器,數字從0到n-1。
>>>for num in range(4): print(num) 0 1 2 3 >>>
6、代碼格式錯誤處理
在IDLE中按F5後有提示Inconsistent indentation detected等,表示製表符和空格混合縮進的錯誤時,可用在IDLE中菜單欄選擇Format-->點擊Tabify Region。可用自動將縮進所有改成製表符。