Python

這部分主要是參考The Python Standard Library的內容所撰寫。

程式語言的3個基礎

資料型態(Data Type)

在開始介紹資料型態之前,先介紹Python內建的常數,注意這些常數所屬的資料型別,總共有6個:

    • False: types.BooleanType

    • True: types.BooleanType

    • None: types.NoneType

    • NotImplemented: types.NotImplementedType

    • Ellipsis: types.EllipsisType

    • __debug__: types.BooleanType,其值一般是True,可以使用「-O」命令列選項改變為False,意思是最佳化Python的bytecode。

另外,site模組也有定義5個常數,一般而言Python都會預設匯入使用。若執行python不要匯入site模組,則使用「-S」命令列選項。

    • quit: site.Quitter,這個常數是可呼叫的(callable),定義在Quitter類別。

    • exit: site.Quitter,這個常數是可呼叫的(callable),定義在Quitter類別。

    • copyritht: site._Printer

    • license: site._Printer

    • credits: site._Printer

Python內建資料型別的數量可以參照types模組所定義的內建型別名稱(name),使用dir(types)就可以知道有哪些型別名稱,總共有37個名稱,實際上使用的資料型別只有28個,若要將資料型別分類,大致上可以分成下列5類:

    • 數值(numeric)型別:bool, int, float, long, complex

    • 序列(sequence)型別:str, unicode, (basestring), list, tuple, (enumerate), bytearray, buffer, xrange

    • 集合(set)型別:set, frozenset

    • 對應(mapping)型別:dict

    • 其他:object, type, file, memoryview, module, class, function, method, (property, classmethod, staticmethod, super, buffer)

這些資料型別當中,依據操作方式可以分成「可變更(mutable)」和「不可變更(immutable)」兩種型別:

    • mutable:list, bytearray, set, dict

    • immutable:str, unicode, list, frozenset

資料型別清單:

    1. basestring

    2. bool

    3. buffer

    4. bytearray

    5. bytes

    6. classmethod

    7. complex

    8. dict

    9. enumerate

    10. file

    11. float

    12. frozenset

    13. int

    14. list

    15. long

    16. memoryview

    17. object

    18. property

    19. reversed

    20. set

    21. slice

    22. staticmethod

    23. str

    24. super

    25. tuple

    26. type

    27. unicode

    28. xrange

內建例外總共有48個,但在Windows平台中多一個WindowsError例外。

    1. ArithmeticError

    2. AssertionError

    3. AttributeError

    4. BaseException

    5. BufferError

    6. BytesWarning

    7. DeprecationWarning

    8. EOFError

    9. EnvironmentError

    10. Exception

    11. FloatingPointError

    12. FutureWarning

    13. GeneratorExit

    14. IOError

    15. ImportError

    16. ImportWarning

    17. IndentationError

    18. IndexError

    19. KeyError

    20. KeyboardInterrupt

    21. LookupError

    22. MemoryError

    23. NameError

    24. NotImplementedError

    25. OSError

    26. OverflowError

    27. PendingDeprecationWarning

    28. ReferenceError

    29. RuntimeError

    30. RuntimeWarning

    31. StandardError

    32. StopIteration

    33. SyntaxError

    34. SyntaxWarning

    35. SystemError

    36. SystemExit

    37. TabError

    38. TypeError

    39. UnboundLocalError

    40. UnicodeDecodeError

    41. UnicodeEncodeError

    42. UnicodeError

    43. UnicodeTranslateError

    44. UnicodeWarning

    45. UserWarning

    46. ValueError

    47. Warning

    48. WindowsError

    49. ZeroDivisionError

上述這些Python預設常數、函式和例外等,都是藉由__builtin__模組取得,或是直接使用dir(__builtins__)。目前2.7.3版本利用dir(__builtins__)指令會列出144個(Windows系統是145個,多一個WindowsError),其中bytes型別是Python 3.0才正式新增,但目前已經加入。144中去除_, __doc__, __name__, __package__這四個屬性,僅有140個。

分支(Branch)

Python語言的分支語法有兩大類「if陳述」和「try陳述」。

if陳述(if statement)

if_stmt ::= "if" expression ":" suite

( "elif" expression ":" suite )*

["else" ":" suite]

try陳述(try statement)

try_stmt ::= try1_stmt | try2_stmt

try1_stmt ::= "try" ":" suite

("except" [expression [("as" | ",") target]] ":" suite)+

["else" ":" suite]

["finally" ":" suite]

try2_stmt ::= "try" ":" suite

"finally" ":" suite

迴圈(Loop)

Python語言的迴圈語法有兩大類「while陳述」和「for陳述」。

while陳述(while statement)

while_stmt ::= "while" expression ":" suite

["else" ":" suite]

for陳述(for statement)

for_stmt ::= "for" target_list "in" expression_list ":" suite

["else" ":" suite]

再利用的問題

函式(Function)

Pyhton內建的函式總共有80個,結構上分為兩大類,其中之一是內建函式,另一類是型別函式,當呼叫型別函式時,其回傳值是一種內建型別,其實型別函式就是型別的建構子。內建函式總共52個,而型別函式的數目等於資料型別數量,即27個,此外,還有一個help函式。

注意,input和raw_input若不是在Interactive Mode的話,它的型別將會是FunctionType。

    1. abs(), 內建函式

    2. all(), 內建函式

    3. any(), 內建函式

    4. basestring(), 型別

    5. bin(), 內建函式

    6. bool(), 型別

    7. bytearray(), 型別

    8. callable(), 內建函式

    9. chr(), 內建函式

    10. classmethod(), 型別

    11. cmp(), 內建函式

    12. compile(), 內建函式

    13. complex(), 型別

    14. delattr(), 內建函式

    15. dict(), 型別

    16. dir(), 內建函式

    17. divmod(), 內建函式

    18. enumerate(), 型別

    19. eval(), 內建函式

    20. execfile(), 內建函式

    21. file(), 型別

    22. filter(), 內建函式

    23. float(), 型別

    24. format(), 內建函式

    25. frozenset(), 型別

    26. getattr(), 內建函式

    27. globals(), 內建函式

    28. hasattr(), 內建函式

    29. hash(), 內建函式

    30. help(),由site模組加入至built-in名稱空間之中,是site模組中定義的一個函式。

    31. hex(), 內建函式

    32. id(), 內建函式

    33. input(), 內建函式

    34. int(), 型別

    35. isinstance(), 內建函式

    36. issubclass(), 內建函式

    37. iter(), 內建函式

    38. len(), 內建函式

    39. list(), 型別

    40. locals(), 內建函式

    41. long(), 型別

    42. map(), 內建函式

    43. max(), 內建函式

    44. memoryview(), 型別

    45. min(), 內建函式

    46. next(), 內建函式

    47. object(), 型別

    48. oct(), 內建函式

    49. open(), 內建函式

    50. ord(), 內建函式

    51. pow(), 內建函式

    52. print(), 內建函式

    53. property(), 型別

    54. range(), 內建函式

    55. raw_input(), 內建函式

    56. reduce(), 內建函式

    57. reload(), 內建函式

    58. repr(), 內建函式

    59. reversed(), 型別

    60. round(), 內建函式

    61. set(), 型別

    62. setattr(), 內建函式

    63. slice(), 型別

    64. sorted(), 內建函式

    65. staticmethod(), 型別

    66. str(), 型別

    67. sum(), 內建函式

    68. super(), 型別

    69. tuple(), 型別

    70. type(), 型別

    71. unichr(), 內建函式

    72. unicode(), 型別

    73. vars(), 內建函式

    74. xrange(), 型別

    75. zip(), 內建函式

    76. __import__(), 內建函式

    77. apply(), 內建函式, 非必需的(Non-essential)

    78. buffer(), 型別, 非必需的(Non-essential)

    79. coerce(), 內建函式, 非必需的(Non-essential)

    80. intern(), 內建函式, 非必需的(Non-essential)

程式庫(Library)

除了內建的程式庫之外,Pyhton提供的標準程式庫相當多,去除特定平台,大致上可以分成26類服務。

    1. String Services

    2. Data Types

    3. Numeric and Mathematical Modules

    4. File and Directory Access

    5. Data Persistence

    6. Data Compression and Archiving

    7. File Formats

    8. Cryptographic Services

    9. Generic Operating System Services

    10. Optional Operating System Services

    11. Interprocess Communication and Networking

    12. Internet Data Handling

    13. Structured Markup Processing Tools

    14. Internet Protocols and Support

    15. Multimedia Services

    16. Internationalization

    17. Program Frameworks

    18. Graphical User Interfaces with Tk

    19. Development Tools

    20. Debugging and Profiling

    21. Python Runtime Services

    22. Custom Python Interpreters

    23. Restricted Execution

    24. Importing Modules

    25. Python Language Services

    26. Miscellaneous Services