Pythonによるオブジェクト指向プログラミング入門
George Boorman
Curriculum Manager, DataCamp



$$\Large{\text{オブジェクト = データ + 機能}}$$

状態 —— オブジェクトのデータ
振る舞い —— オブジェクトの_機能_
| オブジェクト | 型 |
|---|---|
5 |
int |
"Hello" |
str |
pd.DataFrame() |
DataFrame |
sum() |
function |
| ... | ... |


lists はクラス[1, 2, 3, 4, 5].append())type() でクラスを確認type([1, 2, 3, 4, 5])
<class 'list'>
import pandas as pd df = pd.DataFrame({"a": [1,2,3], "b": [4,5,6]})# shape 属性 df.shape
(3, 2)
obj. で属性・メソッドにアクセスimport pandas as pd df = pd.DataFrame({"a": [1,2,3], "b": [4,5,6]})# head メソッド df.head()
a b
0 1 4
1 2 5
2 3 6
# 属性とメソッドを表示
dir([1, 2, 3, 4])
['__add__',
'__class__',
'__contains__',
'__delattr__',
...
'pop',
'remove',
'reverse',
'sort']
# 属性とメソッドを表示
dir(list)
['__add__',
'__class__',
'__contains__',
'__delattr__',
...
'pop',
'remove',
'reverse',
'sort']
| 用語 | 定義 |
|---|---|
| クラス | オブジェクトを作る設計図/テンプレート |
| オブジェクト | データと_機能_の組み合わせ。クラスのインスタンス |
| 状態 | オブジェクトに紐づくデータ。属性で設定 |
| 振る舞い | オブジェクトの_機能_。メソッドで定義 |
Pythonによるオブジェクト指向プログラミング入門