オブジェクト指向プログラミングの基礎

Python 中級オブジェクト指向プログラミング

Jake Roach

Data Engineer

クラスの定義

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age
    self.height = 0

# This invokes a call to __init__
john = Person("John Casey", 38)
  • class キーワードでクラスを定義
  • __init__ はコンストラクタ。新しいインスタンス作成時に呼ばれる
  • self はそのインスタンス自身を指す
Python 中級オブジェクト指向プログラミング

インスタンス属性

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age
# Create an instance of Person
sarah = Person("Sarah Walker", 31)
sarah.age  # Retrieve the age instance attribute
  • クラスのオブジェクトに結びつく
  • self.<attribute-name> 構文で設定・取得
  • <object-name>.<attribute-name> でアクセス
Python 中級オブジェクト指向プログラミング

クラス属性

  • クラス自体に属する
  • オブジェクトなしで取得可能
  • すべてのインスタンスで共通のデータを保持
class Person:
  residence = "Planet Earth"
  ...
# Accessed without an instance
print(Person.residence)
Planet Earth
Python 中級オブジェクト指向プログラミング

インスタンスメソッド

class Person:
  ...
  def introduce(self):
      print(f"Hello, my name is {self.name}")
chuck = Person("Chuck", 32)
chuck.introduce()  # Called on a Person object

$$

  • 呼び出しにはインスタンスが必要
  • 最初の引数は self
Python 中級オブジェクト指向プログラミング

クラスメソッド

class Person:
  @classmethod
  def wake_up(cls):
      print("Time to start your day!")
# Calling a class method
Person.wake_up()

$$

  • @classmethod で装飾
  • 呼び出しにクラスのインスタンスは不要
Python 中級オブジェクト指向プログラミング

継承

継承によりクラス間でコードを再利用できる

  • 子クラス(Employee)は親クラス(Person)の機能を継承
  • 「is-a」関係
  • 追加の機能を実装可能
    • 属性
    • メソッド
class Person:
  def __init__(self, name, age):
    self.name
    self.age = age

  def introduce(self):
      print(f"Hello, my name is {self.name}")
class Employee(Person):
  def __init__(self, name, age, title):
    Person.__init__(self, name, age)
    self.title = title

  def change_position(self, new_title):
      self.title = new_title
Python 中級オブジェクト指向プログラミング

継承

lester = Employee("Lester", 26, "Technician")
lester.introduce()  # Inherited from Person
print(lester.title)
Hello, my name is Lester
Technician
lester.change_position("Cashier")
print(lester.title)
Cashier
Python 中級オブジェクト指向プログラミング

super()

class Employee(Person):
  def __init__(self, name, age, title):
    # Uses name of the parent class
    Person.__init__(self, name, age)

    self.title = title
  ...
  • クラス名と __init__() を直接呼ぶ方法

super() を使う

class Employee(Person):
  def __init__(self, name, age, title):
    # クラス名の代わりに super()
    super().__init__(name, age)

    self.title = title
  ...
  • super()__init__()
  • self を渡す必要はない
Python 中級オブジェクト指向プログラミング

オーバーライド

子クラスが継承したメソッドを別の実装で上書きする(override)

class Employee(Person):
  ...
  def introduce(self):
      print(f"""My name is {self.name},
        I am a {self.title}""")
lester = Employee("Lester", 26, "Technician")
lester.introduce()
My name is Lester, I am a Technician
Python 中級オブジェクト指向プログラミング

オーバーロード

class Person:
  def __init__(self, name):
      self.name = name

  def __eq__(self, other):
      return self.name == other.name

$$

  • クラスに対して Python 演算子の挙動をカスタマイズ
  • __eq__()== をオーバーロードする

$$

$$

$$

$$

# Compare two Person objects
chuck = Person("Charles Carmichael")
charles = Person("Charles Carmichael")
print(chuck == charles)
True
Python 中級オブジェクト指向プログラミング

Passons à la pratique !

Python 中級オブジェクト指向プログラミング

Preparing Video For Download...