More Related Content
Similar to Programming python - part 2
Similar to Programming python - part 2 (20)
Programming python - part 2
- 2. 2
@
大綱
Chapter 7 類別
Chapter 8 例外處理
Chapter 9 常用模組介紹
- 4. 4
@
類別
基本格式
類別內建屬性
建構元
方法
成員
類別變數
實例變數
實例變數內建屬性
- 5. 5
@
類別 –– 基本格式
class <classname>:
“””document….
……………………”””
statement….
class <classname>[(class,class…class)]
“””document…….
………………………..”””
statement
- 6. 6
@
類別內建屬性
格式: classname.內建屬性
__dict__:將內建屬性以Dictionary格式儲存
__doc__:取得class內所撰寫的說明文字
__name__:取得class自己的名稱
__module__:取得class來自哪個模組
__bases__:取得class繼承自哪些模組
- 7. 7
@
類別內建屬性 -- ____ddiicctt____
將內建屬性以Dictionary格式儲存
- 8. 8
@
類別內建屬性 -- ____ddoocc____
__doc__:取得class內所撰寫的說明文字
- 9. 9
@
類別內建屬性 -- ____nnaammee____
__name__:取得class自己的名稱
- 12. 12
@
建構元
建構一個物件並賦予其屬性的方法
Overload __init__ method
- 17. 17
@
實例變數內建屬性
格式: instance.內建屬性
__dict__:將instance內的屬性以Dict格式儲存
__class__:取得instance的所屬類別
- 18. 18
@
類別內建屬性 -- ____ddiicctt____
將內建屬性以Dictionary格式儲存
- 25. 25
@
類別的繼承 -- 多重繼承
格式 class class_name(class1,
class2….classN):
- 26. 26
@
類別的繼承 -- 多重繼承的順序
如果繼承的集合成員,擁有相同方法,則該類別
繼承類別方法的順序從左至右
- 30. 30
@
類別的封裝
Python內所有的屬性與方法都是public
命名時以 ‘ _’ (一個底線)作為開頭, 被視為類
別的內部
屬性、方法
命名時以’ __’(兩個底線)作為開頭, 被視為類別
的私有
屬性、方法
- 32. 32
@
類別的名稱空間
類別的所有屬性都儲存在該類別的名稱空間
若於類別內儲存一個全域變數的值, 當全域變數
改變,
類別將不會跟著改變
- 37. 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)
- 38. 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)
- 39. 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)
- 40. 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)
- 41. 41
@
類別內建方法
__setslice__(self, index1, index2, list)
__delslice__(self, index1, index2)
- 42. 42
@
類別內建方法 -- ____iinniitt____((sseellff))
類別建構元
可以利用不定引述的寫法,完成不同參數的建構
元
- 43. 43
@
類別內建方法 -- ____ssttrr____((sseellff))
設定當物件被指定為字串時,應該回傳的字串
- 44. 44
@
類別內建方法 -- ____ggeettaattttrr____((sseellff,,
nnaammee))
- 45. 45
@
類別內建方法 ––
____sseettaattttrr____((sseellff,,
nnaammee,, vvaalluuee))
- 46. 46
@
類別內建方法 -- ____ddeellaattttrr____((sseellff,,
nnaammee))
- 51. 51
@
類別內建方法 ––
sseettiitteemm、
gg ee__ttiistteeetimmtem、__(ddseeellfl,ii ttienedmmex, value)
__getitem__(self,index)
__delitem__(self,index)
類似 __setattr__、__getattr__、delattr,但其格
式為
__setitem__: A[‘key’] = c
__getitem__: A[‘key’]
__delitem__: del A[‘key’]
- 52. 52
@
類別內建方法 ––
____ggeettsslliiccee____((sseellff,, iinnddeexx11,,
iinn Sddtree =xx 2‘2a))bcdefg’ Str = Str[2:5]
index1 index2
- 53. 53
@
類別內建方法 ––
____sseettsslliiccee____((sseellff,, iinnddeexx11,, iinnddeexx22,, lliisstt))
- 54. 54
@
類別內建方法 ––
____ddeellsslliiccee____((sseellff,, iinnddeexx11,,
iinnddeexx22))
- 56. 56
@
例外處理 –– 基本格式
try:
statement….
statement…
except: [excetion type[, instance of exception]]:
statement…
[else:
statement…]
[finally:
statement…]
- 62. 62
@
例外處理 –– 產生例外
格式:
1) raise class
2) raise exception, single_arg
3) raise exception, (arg1, arg2, arg3…)
4) raise exception (arg1, arg2, ….)
- 101. 101
@
SSoocckkeett模組 –– TTCCPP 範例
建立socket到指定的本地主機ip, port位置
設定最大連線數
無限回圈, 接受連線
傳送字串給連線端
關閉socket
- 107. 107
@
uurrllppaarrssee模組 –– 解析UURRLL
透過urlparse將傳入的URL字串,分解成
addressing scheme、netword location 、path 、
parameters 、query 、fragment identifier