Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ
George Boorman
Curriculum Manager, DataCamp



$$\Large{\text{Object = data + functionality}}$$

State - ข้อมูลของออบเจกต์
Behavior - ฟังก์ชันการทำงานของออบเจกต์
| ออบเจกต์ | ประเภท |
|---|---|
5 |
int |
"Hello" |
str |
pd.DataFrame() |
DataFrame |
sum() |
function |
| ... | ... |


lists คือ class[1, 2, 3, 4, 5].append()type() เพื่อตรวจสอบ classtype([1, 2, 3, 4, 5])
<class 'list'>
import pandas as pd df = pd.DataFrame({"a": [1,2,3], "b": [4,5,6]})# shape attribute df.shape
(3, 2)
obj. เพื่อเข้าถึงแอตทริบิวต์และเมธอดimport pandas as pd df = pd.DataFrame({"a": [1,2,3], "b": [4,5,6]})# head method df.head()
a b
0 1 4
1 2 5
2 3 6
# Display attributes and methods
dir([1, 2, 3, 4])
['__add__',
'__class__',
'__contains__',
'__delattr__',
...
'pop',
'remove',
'reverse',
'sort']
# Display attributes and methods
dir(list)
['__add__',
'__class__',
'__contains__',
'__delattr__',
...
'pop',
'remove',
'reverse',
'sort']
| คำศัพท์ | ความหมาย |
|---|---|
| Class | แม่แบบ/เทมเพลตสำหรับสร้างออบเจกต์ |
| Object | การรวมกันของข้อมูลและฟังก์ชันการทำงาน คืออินสแตนซ์ของ class |
| State | ข้อมูลที่เชื่อมกับออบเจกต์ กำหนดผ่านแอตทริบิวต์ |
| Behavior | ฟังก์ชันการทำงานของออบเจกต์ กำหนดผ่านเมธอด |
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ