110
Johnson Hsu [email protected] Python Python Programming Programming

Programming python - part 2

Embed Size (px)

Citation preview

Page 1: Programming python - part 2

Johnson Hsu

[email protected]

Python Python ProgrammingProgramming

Page 2: Programming python - part 2

2

@

大綱

Chapter 7 類別 Chapter 8 例外處理 Chapter 9 常用模組介紹

Page 3: Programming python - part 2

3

@

Chapter 7 類別

Page 4: Programming python - part 2

4

@類別類別

基本格式

類別內建屬性

建構元

方法

成員

類別變數

實例變數

實例變數內建屬性

Page 5: Programming python - part 2

5

@ – 類別 基本格式 – 類別 基本格式

class <classname>:

“””document….

……………………”””

statement….

class <classname>[(class,class…class)]

“””document…….

………………………..”””

statement

Page 6: Programming python - part 2

6

@類別內建屬性類別內建屬性

格式 : classname.內建屬性

__dict__:將內建屬性以 Dictionary格式儲存

__doc__:取得 class內所撰寫的說明文字

__name__:取得 class自己的名稱

__module__:取得 class來自哪個模組

__bases__:取得 class繼承自哪些模組

Page 7: Programming python - part 2

7

@ 類別內建屬性 類別內建屬性 -- __dict__-- __dict__

將內建屬性以 Dictionary格式儲存

Page 8: Programming python - part 2

8

@ 類別內建屬性 類別內建屬性 -- __doc__-- __doc__

__doc__:取得 class內所撰寫的說明文字

Page 9: Programming python - part 2

9

@ 類別內建屬性 類別內建屬性 -- __name__-- __name__

__name__:取得 class自己的名稱

Page 10: Programming python - part 2

10

@類別內建屬性類別內建屬性

__module__:取得 class來自哪個模組

Page 11: Programming python - part 2

11

@類別內建屬性類別內建屬性

__bases__:取得 class繼承自哪些模組

Page 12: Programming python - part 2

12

@建構元建構元

建構一個物件並賦予其屬性的方法

Overload __init__ method

Page 13: Programming python - part 2

13

@方法方法

即函式

Page 14: Programming python - part 2

14

@成員成員

Page 15: Programming python - part 2

15

@類別變數類別變數

透過類別直接存取

宣告在方法外

Page 16: Programming python - part 2

16

@實例變數實例變數

格式 : obj = class(arg1, arg2, arg3…argN)

Page 17: Programming python - part 2

17

@實例變數內建屬性實例變數內建屬性

格式 : instance.內建屬性

__dict__:將 instance內的屬性以 Dict格式儲存

__class__:取得 instance的所屬類別

Page 18: Programming python - part 2

18

@ 類別內建屬性 類別內建屬性 -- __dict__-- __dict__

將內建屬性以 Dictionary格式儲存

Page 19: Programming python - part 2

19

@instance.method(arg1, …)instance.method(arg1, …)如何運作如何運作

Instance.method(arg..) Class.method(instance, arg..)

Page 20: Programming python - part 2

20

@instance.method(arg1, …)instance.method(arg1, …)如何運作如何運作

Page 21: Programming python - part 2

21

@類別方法的類別方法的 OverloadingOverloading

重寫類別內原來的方法

擴充類別內原來的方法

Page 22: Programming python - part 2

22

@類別方法的類別方法的 OverloadingOverloading

重寫類別內原來的方法

Page 23: Programming python - part 2

23

@類別方法的類別方法的 OverloadingOverloading

擴充類別內原來的方法

Page 24: Programming python - part 2

24

@類別的繼承類別的繼承

多重繼承

多重繼承的順序

指定繼承哪個類別的方法

Page 25: Programming python - part 2

25

@ 類別的繼承 類別的繼承 -- -- 多重繼承多重繼承

格式 class class_name(class1,

class2….classN):

Page 26: Programming python - part 2

26

@ 類別的繼承 類別的繼承 -- -- 多重繼承的順序多重繼承的順序

如果繼承的集合成員,擁有相同方法,則該類別繼承類別方法的順序從左至右

Page 27: Programming python - part 2

27

@ 類別的繼承 類別的繼承 -- -- 指定繼承哪個類別的指定繼承哪個類別的

方法方法 指定繼承哪個類別的方法

Page 28: Programming python - part 2

28

@類別的多型類別的多型

並沒有真正的多型

借由不訂引數判定該呼叫的函式達成

Page 29: Programming python - part 2

29

@類別的多型類別的多型

Page 30: Programming python - part 2

30

@類別的封裝類別的封裝

Python內所有的屬性與方法都是 public

‘命名時以 _’ (一個底線 )作為開頭 , 被視為類

別的內部

屬性、方法

’命名時以 __’(兩個底線 )作為開頭 , 被視為類別

的私有

屬性、方法

Page 31: Programming python - part 2

31

@類別的封裝類別的封裝

Page 32: Programming python - part 2

32

@類別的名稱空間類別的名稱空間

類別的所有屬性都儲存在該類別的名稱空間

若於類別內儲存一個全域變數的值 , 當全域變數

改變 ,

類別將不會跟著改變

Page 33: Programming python - part 2

33

@類別變數與成員在實例變數的搜尋順類別變數與成員在實例變數的搜尋順序序

Page 34: Programming python - part 2

34

@類別變數與成員在實例變數的搜尋順類別變數與成員在實例變數的搜尋順序序

Module Not Found

Page 35: Programming python - part 2

35

@類別變數與成員在實例變數的搜尋順類別變數與成員在實例變數的搜尋順序序

Module Not Found

Page 36: Programming python - part 2

36

@類別變數與成員在實例變數的搜尋順類別變數與成員在實例變數的搜尋順序序

1

2

Page 37: Programming python - part 2

37

@類別內建方法類別內建方法

__init__(self)

__str__(self)

__repr__(self)

__getattr__(self, name)

__setattr__(self, name, value)

__delattr__(self, name)

__del__(self)

__cmp__(self, other)

__hash__(self)

__nonzero__(self)

Page 38: Programming python - part 2

38

@類別內建方法類別內建方法

__call__(self)

__getitem__(self, index)

__len__(self)

__add__(self, other)

__iadd__(self, other)

__sub__(self, other)

__isub__(self, other)

__mul__(self, other)

__imul__(self, other)

__div__(self, other)

__idiv__(self, other)

Page 39: Programming python - part 2

39

@類別內建方法類別內建方法

__mod__(self, other)

__imod__(self, other)

__neg__(self)

__pos__(slef)

__abs__(self)

__inv__(self)

__Ishift__(self, other)

__iIshift__(self, other)

__rshift__(self, other)

__irshift__(self, other)

Page 40: Programming python - part 2

40

@類別內建方法類別內建方法

__and__(self, other)

__isand__(self, other)

__or__(self, other)

__ior__(self, other)

__xor__(self, other)

__ixor__(self, other)

__not__(self, other)

__setitem__(self, index, value)

__delitem__(self,index)

__getslice__(self, index1, index2)

Page 41: Programming python - part 2

41

@類別內建方法類別內建方法

__setslice__(self, index1, index2, list)

__delslice__(self, index1, index2)

Page 42: Programming python - part 2

42

@ 類別內建方法 類別內建方法 -- __init__(self)-- __init__(self)

類別建構元

可以利用不定引述的寫法,完成不同參數的建構元

Page 43: Programming python - part 2

43

@ 類別內建方法 類別內建方法 -- __str__(self)-- __str__(self)

設定當物件被指定為字串時,應該回傳的字串

Page 44: Programming python - part 2

44

@ 類別內建方法 類別內建方法 -- __getattr__(self, -- __getattr__(self,

name)name)

Page 45: Programming python - part 2

45

@ – 類別內建方法 – 類別內建方法

__setattr__(self, __setattr__(self, name, value)name, value)

Page 46: Programming python - part 2

46

@ 類別內建方法 類別內建方法 -- __delattr__(self, -- __delattr__(self,

name)name)

Page 47: Programming python - part 2

47

@ 類別內建方法 類別內建方法 -- __del__(self)-- __del__(self)

Page 48: Programming python - part 2

48

@ 類別內建方法 類別內建方法 -- __del__(self)-- __del__(self)

Page 49: Programming python - part 2

49

@ 類別內建方法 類別內建方法 -- __call__(self)-- __call__(self)

Page 50: Programming python - part 2

50

@ 類別內建方法 類別內建方法 -- __call__(self)-- __call__(self)

Page 51: Programming python - part 2

51

@ – 類別內建方法 – 類別內建方法

setitemsetitem、、getitemgetitem、、 delitemdelitem __setitem__(self, index, value)

__getitem__(self,index)

__delitem__(self,index)

類似 __setattr__、 __getattr__、 delattr,但其格

式為

__setitem__: A[‘key’] = c

__getitem__: A[‘key’]

__delitem__: del A[‘key’]

Page 52: Programming python - part 2

52

@ – 類別內建方法 – 類別內建方法

__getslice__(self, index1, __getslice__(self, index1, index2)index2) Str = ‘abcdefg’ Str = Str[2:5]

index1 index2

Page 53: Programming python - part 2

53

@ – 類別內建方法 – 類別內建方法

__setslice__(self, index1, index2, list)__setslice__(self, index1, index2, list)

Page 54: Programming python - part 2

54

@ – 類別內建方法 – 類別內建方法

__delslice__(self, index1, __delslice__(self, index1, index2)index2)

Page 55: Programming python - part 2

55

@

Chapter 8 例外處理

Page 56: Programming python - part 2

56

@ – 例外處理 基本格式 – 例外處理 基本格式

try:

statement….

statement…

except: [excetion type[, instance of exception]]:

statement…

[else:

statement…]

[finally:

statement…]

Page 57: Programming python - part 2

57

@例外處理例外處理

Page 58: Programming python - part 2

58

@例外處理例外處理

Page 59: Programming python - part 2

59

@例外處理例外處理

Page 60: Programming python - part 2

60

@ – 例外處理 例外實例變數的 – 例外處理 例外實例變數的 argsargs屬屬

性性

Tuple

Page 61: Programming python - part 2

61

@ – 例外處理 忽略例外 – 例外處理 忽略例外

Page 62: Programming python - part 2

62

@ – 例外處理 產生例外 – 例外處理 產生例外

格式:

    1) raise class

2) raise exception, single_arg

3) raise exception, (arg1, arg2, arg3…)

4) raise exception (arg1, arg2, ….)

Page 63: Programming python - part 2

63

@ – 例外處理 產生例外 – 例外處理 產生例外

Page 64: Programming python - part 2

64

@ – 例外處理 自訂例外 – 例外處理 自訂例外

Page 65: Programming python - part 2

65

@ – 例外處理 自訂例外 – 例外處理 自訂例外

Page 66: Programming python - part 2

66

@

Chapter 9 常用模組介紹

Page 67: Programming python - part 2

67

@StringString – 模組 尋找字串的位置 – 模組 尋找字串的位置

Page 68: Programming python - part 2

68

@StringString – 模組 搜尋並取代字串 – 模組 搜尋並取代字串

Page 69: Programming python - part 2

69

@StringString – 模組 刪除換行字元 – 模組 刪除換行字元

Page 70: Programming python - part 2

70

@StringString – 模組 英文字母 – 模組 英文字母 2626個常數個常數

Page 71: Programming python - part 2

71

@StringString – 模組 將字串轉為小寫 – 模組 將字串轉為小寫

Page 72: Programming python - part 2

72

@StringString – 模組 指定分隔字元拆解此 – 模組 指定分隔字元拆解此字串字串

Page 73: Programming python - part 2

73

@StringString – 模組 – 模組 splitsplit預設分隔字元為預設分隔字元為空白空白

Page 74: Programming python - part 2

74

@StringString – 模組 將 – 模組 將 listlist內的集合組成字內的集合組成字串串 並且指定 並且指定分隔字元分隔字元

Page 75: Programming python - part 2

75

@StringString – 模組 將 – 模組 將 listlist內的集合組成字內的集合組成字串串

Page 76: Programming python - part 2

76

@StringString – 模組 另一種取代方式 – 模組 另一種取代方式

Page 77: Programming python - part 2

77

@StringString – 模組 轉型 – 模組 轉型

Page 78: Programming python - part 2

78

@syssys – 模組 系統相關資訊 – 模組 系統相關資訊

Page 79: Programming python - part 2

79

@syssys – 模組 模組的搜尋路徑 – 模組 模組的搜尋路徑

Page 80: Programming python - part 2

80

@syssys – 模組 新增搜尋環境到目前環境 – 模組 新增搜尋環境到目前環境中中

Page 81: Programming python - part 2

81

@syssys – 模組 取得目前載入的模組 – 模組 取得目前載入的模組

Page 82: Programming python - part 2

82

@syssys – 模組 取得執行時的參數 – 模組 取得執行時的參數

Page 83: Programming python - part 2

83

@syssys – 模組 將輸入參數存成 – 模組 將輸入參數存成 dictdict物物件件

Page 84: Programming python - part 2

84

@osos – 模組 取得程式於系統的 – 模組 取得程式於系統的process idprocess id

Page 85: Programming python - part 2

85

@osos – 模組 取得目前路徑、切換目錄 – 模組 取得目前路徑、切換目錄

Page 86: Programming python - part 2

86

@osos – 模組 – 模組 路徑之間分隔字元、目錄的路徑之間分隔字元、目錄的

分隔字元、上層目錄、目前目錄、換行分隔字元、上層目錄、目前目錄、換行

字元字元

Page 87: Programming python - part 2

87

@osos – 模組 判斷是否為資料夾、檔案 – 模組 判斷是否為資料夾、檔案、取得檔案大小、判斷是否存在、取得檔案大小、判斷是否存在

Page 88: Programming python - part 2

88

@osos – 模組 分離檔案名稱與路徑並儲 – 模組 分離檔案名稱與路徑並儲存成存成 tupletuple

Page 89: Programming python - part 2

89

@osos – 模組 組合路徑與檔案 – 模組 組合路徑與檔案

Page 90: Programming python - part 2

90

@osos – 模組 各別取出檔案路徑與檔案 – 模組 各別取出檔案路徑與檔案名稱名稱

Page 91: Programming python - part 2

91

@osos – 模組 取得副檔名 – 模組 取得副檔名

Page 92: Programming python - part 2

92

@osos – 模組 取得絕對路徑 – 模組 取得絕對路徑

Page 93: Programming python - part 2

93

@osos – 模組 執行系統 – 模組 執行系統 shellshell指令指令

Page 94: Programming python - part 2

94

@osos – 模組 取得環境變數 – 模組 取得環境變數

Page 95: Programming python - part 2

95

@osos – 模組 將路徑隔別取出來 – 模組 將路徑隔別取出來

Page 96: Programming python - part 2

96

@osos – 模組 取得指定路徑的檔案、資 – 模組 取得指定路徑的檔案、資料夾料夾

Page 97: Programming python - part 2

97

@osos – 模組 取得目錄下所有檔案與資 – 模組 取得目錄下所有檔案與資料夾料夾

Page 98: Programming python - part 2

98

@osos –模組 取得目錄下所有檔案與資料 –模組 取得目錄下所有檔案與資料夾夾

Page 99: Programming python - part 2

99

@osos –模組 取得目錄下所有檔案與資料 –模組 取得目錄下所有檔案與資料夾夾

Page 100: Programming python - part 2

100

@osos –模組 取得目錄下所有檔案與資料 –模組 取得目錄下所有檔案與資料夾夾

Page 101: Programming python - part 2

101

@SocketSocket – 模組 – 模組 TCP TCP 範例範例

建立 socket到指定的本地主機 ip, port位置

設定最大連線數

無限回圈 , 接受連線

傳送字串給連線端

關閉 socket

Page 102: Programming python - part 2

102

@SocketSocket – 模組 – 模組 UDP UDP 範例範例

建立 socket到指定的本地主機 ip, port位置

設定接收訊息的資料量大小

取得資料 , 與連線端位置

Page 103: Programming python - part 2

103

@urlliburllib – 模組 取得 – 模組 取得 headersheaders

開啟指定網頁位置

讀入整個網頁

利用該物件取得 header

Page 104: Programming python - part 2

104

@urlliburllib – 模組 特殊字串編碼 – 模組 特殊字串編碼

將字串傳入 quote函式

Page 105: Programming python - part 2

105

@urlliburllib – 模組 特殊字串編碼 – 模組 特殊字串編碼

將字串傳入 quote函式,將對於 URL而言屬於特

殊字

元的部分進行編碼

Page 106: Programming python - part 2

106

@urlliburllib – 模組 反解編碼過後的字串 – 模組 反解編碼過後的字串

將重新編碼過後的字串利用 unquote解回字串原

Page 107: Programming python - part 2

107

@urlparseurlparse – 模組 解析 – 模組 解析 URLURL

透過 urlparse將傳入的 URL字串,分解成

addressing scheme、 netword location 、 path 、

parameters 、 query 、 fragment identifier

Page 108: Programming python - part 2

108

@urlparseurlparse – 模組 反解析 – 模組 反解析 URLURL

將要組成的元素以 tuple方式儲存

呼叫 urlunparse反組成一個完整 URL

Page 109: Programming python - part 2

109

@TelnetTelnet模組模組

開啟連線

登入

執行命令

Page 110: Programming python - part 2

110

@FTPFTP模組模組

開啟連線

登入

執行命令