"""frist line second line""" |
三引號字串可以跨行,印出時會以字串 (string) 原始編排形式顯示。
我們先來看看一些常用資料型態 (data type) 的例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | a = 22 #1 print (a.__doc__) print () a = 22.0 #2 print (a.__doc__) print () a = "22" #3 print (a.__doc__) print () a = [ 22 ] #4 print (a.__doc__) # 《程式語言教學誌》的範例程式 # 檔名:cla09.py # 功能:示範 Python 程式 # 作者:張凱慶 # 時間:西元 2010 年 12 月 |
執行結果如下

第 1 組
1 2 | a = 22 #1 print (a.__doc__) |
a 為整數 (integer) ,另有內建函數 (function) int() 可將其他型態轉換為整數。
第 2 組
6 7 | a = 22.0 #2 print (a.__doc__) |
a 為浮點數 (floating-point number) ,另有內建函數 float() 可將其他型態轉換為浮點數。
第 3 組
11 12 | a = "22" #3 print (a.__doc__) |
a 為字串,另有內建函數 str() 可將其他型態轉換為字串。
第 4 組
16 17 | a = [ 22 ] #4 print (a.__doc__) |
a 為串列,另有內建函數 list() 可將其他型態轉換為串列 (list) 。
下例我們替 Demo 加入 __doc__ 屬性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | class Demo: """ Demo - learning Python class...""" def __init__( self , i): self .i = i def __str__( self ): return str ( self .i) def hello( self ): print ( "hello" , self .i) a = Demo( 1122 ) a.hello() print () print (a.__doc__) # 《程式語言教學誌》的範例程式 # 檔名:cla10.py # 功能:示範 Python 程式 # 作者:張凱慶 # 時間:西元 2010 年 12 月 |
執行結果如下

通常三引號字串會直接放在 class 下方,利用 __doc__ 即可存取。
中英文術語對照 | |
---|---|
類別 | class |
屬性 | attribute |
字串 | string |
資料型態 | data type |
整數 | integer |
函數 | function |
浮點數 | floating-point number |
串列 | list |
參考資料
http://docs.python.org/py3k/tutorial/introduction.html
http://docs.python.org/py3k/tutorial/classes.html
http://docs.python.org/py3k/reference/compound_stmts.html
http://docs.python.org/py3k/tutorial/introduction.html
http://docs.python.org/py3k/tutorial/classes.html
http://docs.python.org/py3k/reference/compound_stmts.html
沒有留言:
張貼留言