Python 文件,給自己的程式寫文件
文件,這個詞語在經常在程式設計師的嘴裡冒出來,有時候他們還經常以文件有沒有或者全不全為標準來衡量一個軟體專案是否高大上。那麼,軟體中的文件是什麼呢?有什麼要求呢?python文件又是什麼呢?文件有什麼用呢?
文件,說白了就是用word(這個最多了)等(注意這裡的等,把不常用的工具都等掉了,包括我編輯文字時用的vim工具)文字編寫工具寫成的包含文字內容但不限於文字的檔案。來一個更讓人信服的定義,當然是來自維基百科。
軟體文件或者原始碼文件是指與軟體系統及其軟體工程過程有關聯的文字實體。文件的型別包括軟體需求文件,設計文件,測試文件,使用者手冊等。其中的需求文件,設計文件和測試文件一般是在軟體開發過程中由開發者寫就的,而使用者手冊等非過程類文件是由專門的非技術類寫作人員寫就的。
早期的軟體文件主要指的是使用者手冊,根據Barker的定義,文件是用來對軟體系統介面元素的設計、規劃和實現過程的記錄,以此來增強系統的可用性。而Forward則認為軟體文件是被軟體工程師之間用作溝通交流的一種方式,溝通的資訊主要是有關所開發的軟體系統。Parnas則強調文件的權威性,他認為文件應該提供對軟體系統的精確描述。
綜上,我們可以將軟體文件定義為:
1.文件是一種對軟體系統的書面描述; 2.文件應當精確地描述軟體系統; 3.軟體文件是軟體工程師之間用作溝通交流的一種方式; 4.文件的型別有很多種,包括軟體需求文件,設計文件,測試文件,使用者手冊等; 5.文件的呈現方式有很多種,可以是傳統的書面文字形式或圖表形式,也可是動態的網頁形式
那麼這裡說的Python文件指的是什麼呢?一個方面就是每個學習者要學習python,python的開發者們(他們都是大牛)給我們這些小白提供了什麼東西沒有?能夠讓我們給他們這些大牛溝通,理解python中每個函式、指令等的含義和用法呢?
檢視python文件
文件勝過了所有的教程和所有的老師以及所有的大牛。
python文件的網址:https://docs.python.org/2/,這是python2.x,從這裡也可以找到python3.x的文件。
除了看網站上的文件,還有就是dir()和help()
>>> dir(list) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>> help(list.__mul__) Help on wrapper_descriptor: __mul__(...) x.__mul__(n) <==> x*n
這種檢視文件的方式,在互動模式下經常用到,快捷方便,請看官務必牢記並使用。
正如前面已經介紹過的,還有一個文件:doc,help呼叫的其實就是這個函式裡面的內容。
>>> print(list.__mul__.__doc__) #與help(list.__mul__)顯示的內容一致
x.__mul__(n) <==> x*n
>>> print(list.index.__doc__) #檢視index的文件
L.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
給自己的程式加上文件
在自己編寫程式的時候,也非常希望能夠有類似上面檢視python文件的功能,可以通過某種方式檢視自己的程式文件,這樣顯得自己多牛呀。
有一種方法可以實現,就是在你所編寫的程式中用三個雙引號或者單引號成對地出現,中間寫上有關文件內容。
>>> def hiekay():
... """I like python"""
... print "http://hiekay.github.io"
...
>>> hiekay()
http://hiekay.github.io
>>> print(hiekay.__doc__) #用這種方法可以看自己寫的函式中的文件
I like python
>>> help(hiekay) #其實就是呼叫__doc__顯示的內容
Help on function hiekay in module __main__:
hiekay()
I like python
另外,對於一個檔案,可以把有關說明放在檔案的前面,不影響該檔案程式碼執行。
例如,有這樣一個副檔名是.py的python檔案,其內容是:
#!/usr/bin/env python
#coding:utf-8
import random
number = random.randint(1,100)
guess = 0
while True:
num_input = raw_input("please input one integer that is in 1 to 100:")
guess +=1
if not num_input.isdigit():
print "Please input interger."
elif int(num_input)<0 and int(num_input)>=100:
print "The number should be in 1 to 100."
else:
if number==int(num_input):
print "OK, you are good.It is only %d, then you successed."%guess
break
elif number>int(num_input):
print "your number is more less."
elif number<int(num_input):
print "your number is bigger."
else:
print "There is something bad, I will not work"
如果要對這段程式寫一個文件,就可以這麼做。
"""
This is a game.
I am hiekay.
I like python.
I am writing python articles in my website.
My website is http://hiekay.github.io
You can learn python free in it.
"""
#!/usr/bin/env python
#coding:utf-8
import random
number = random.randint(1,100)
guess = 0
while True:
num_input = raw_input("please input one integer that is in 1 to 100:")
guess +=1
if not num_input.isdigit():
print "Please input interger."
elif int(num_input)<0 and int(num_input)>=100:
print "The number should be in 1 to 100."
else:
if number==int(num_input):
print "OK, you are good.It is only %d, then you successed."%guess
break
elif number>int(num_input):
print "your number is more less."
elif number<int(num_input):
print "your number is bigger."
else:
print "There is something bad, I will not work"