Python 物件導向程式設計
Alex Yarosh
Content Quality Analyst @ DataCamp


$$\Large{\text{Object = state + behavior}}$$

$$\text{\textbf{Encapsulation} - bundling data with code operating on it}$$


type() 查類別import numpy as np
a = np.array([1,2,3,4])
print(type(a))
numpy.ndarray
| Object | Class |
|---|---|
5 |
int |
"Hello" |
str |
pd.DataFrame() |
DataFrame |
np.mean |
function |
| ... | ... |
import numpy as np a = np.array([1,2,3,4])# shape attribute a.shape
(4,)
obj. 存取屬性與方法import numpy as np a = np.array([1,2,3,4])# reshape method a.reshape(2,2)
array([[1, 2],
[3, 4]])
屬性 ↔ 變數 ↔ obj.my_attribute,
方法 ↔ function() ↔ obj.my_method()。
import numpy as np
a = np.array([1,2,3,4])
dir(a) # <--- 列出所有屬性與方法
['T',
'__abs__',
...
'trace',
'transpose',
'var',
'view']
Python 物件導向程式設計