【Python入門】super()を初心者向けに完全解説!継承での親クラス呼び出しの使い方
Pythonのsuper()は、クラス継承において親クラスのメソッドを呼び出すための重要な組み込み関数です。オブジェクト指向プログラミングにおいて、既存のコードを再利用しながら機能を拡張する際に欠かせない機能です。この記事では、super()の基本から実践的な使い方まで、初心者の方にも分かりやすく解説します。
super()とは?
super()は、現在のクラスの親クラス(スーパークラス)のメソッドやプロパティにアクセスするためのPython組み込み関数です。継承関係にあるクラスで、子クラスから親クラスの機能を呼び出したい時に使用します。
super()の役割
- 親クラスのメソッド呼び出し: 親クラスの処理を実行
- 初期化の継承: 親クラスの
__init__メソッドを呼び出し - メソッドの拡張: 親クラスの処理に追加処理を組み合わせ
- 多重継承の対応: 複雑な継承関係でも適切にメソッド解決
基本的なsuper()の使い方
1. 最もシンプルなsuper()
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print(f"{self.name}が鳴いています")
class Dog(Animal):
def __init__(self, name):
super().__init__(name) # 親クラスの初期化を呼び出し
def speak(self):
super().speak() # 親クラスのメソッド呼び出し
print("ワンワン!")
dog = Dog("ポチ")
dog.speak()
# ポチが鳴いています
# ワンワン!
2. 属性の追加とsuper()
class Vehicle:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def info(self):
return f"{self.brand} {self.model}"
class Car(Vehicle):
def __init__(self, brand, model, doors):
super().__init__(brand, model) # 親クラスの初期化
self.doors = doors # 子クラス独自の属性
def info(self):
base_info = super().info() # 親クラスのメソッド結果を取得
return f"{base_info} ({self.doors}ドア)"
car = Car("トヨタ", "プリウス", 4)
print(car.info()) # トヨタ プリウス (4ドア)
3. super()なしの場合との比較
# super()を使わない場合(推奨されない)
class BadExample(Vehicle):
def __init__(self, brand, model, doors):
Vehicle.__init__(self, brand, model) # ハードコーディング
self.doors = doors
# super()を使う場合(推奨)
class GoodExample(Vehicle):
def __init__(self, brand, model, doors):
super().__init__(brand, model) # 柔軟性が高い
self.doors = doors
super()の基本構文
class 親クラス名:
def __init__(self, 引数):
self.属性 = 引数
def メソッド名(self):
処理内容
class 子クラス名(親クラス名):
def __init__(self, 引数, 追加引数):
super().__init__(引数) # 親クラスの初期化
self.追加属性 = 追加引数
def メソッド名(self):
super().メソッド名() # 親クラスのメソッド呼び出し
追加の処理
実践的なsuper()の活用例
1. 社員管理システム
class Employee:
def __init__(self, name, employee_id, salary):
self.name = name
self.employee_id = employee_id
self.salary = salary
def work(self):
print(f"{self.name}が働いています")
def get_info(self):
return f"ID: {self.employee_id}, 名前: {self.name}"
class Manager(Employee):
def __init__(self, name, employee_id, salary, team_size):
super().__init__(name, employee_id, salary)
self.team_size = team_size
def work(self):
super().work() # 基本的な作業
print(f"チーム管理もしています({self.team_size}人)")
def get_info(self):
base_info = super().get_info()
return f"{base_info}, 役職: マネージャー"
class Developer(Employee):
def __init__(self, name, employee_id, salary, programming_language):
super().__init__(name, employee_id, salary)
self.programming_language = programming_language
def work(self):
super().work() # 基本的な作業
print(f"{self.programming_language}でコーディングしています")
manager = Manager("田中", "M001", 600000, 5)
developer = Developer("佐藤", "D001", 500000, "Python")
manager.work()
# 田中が働いています
# チーム管理もしています(5人)
developer.work()
# 佐藤が働いています
# Pythonでコーディングしています
2. ゲームキャラクターシステム
class Character:
def __init__(self, name, hp, attack):
self.name = name
self.hp = hp
self.max_hp = hp
self.attack = attack
self.level = 1
def take_damage(self, damage):
self.hp -= damage
if self.hp < 0:
self.hp = 0
print(f"{self.name}が{damage}ダメージを受けた")
def level_up(self):
self.level += 1
print(f"{self.name}がレベル{self.level}になった")
class Warrior(Character):
def __init__(self, name, hp, attack, defense):
super().__init__(name, hp, attack)
self.defense = defense
def take_damage(self, damage):
reduced_damage = max(1, damage - self.defense)
super().take_damage(reduced_damage) # 軽減されたダメージで親メソッド呼び出し
def level_up(self):
super().level_up() # 基本のレベルアップ処理
self.defense += 2 # 戦士特有の成長
print(f"防御力が{self.defense}に上がった")
class Mage(Character):
def __init__(self, name, hp, attack, mana):
super().__init__(name, hp, attack)
self.mana = mana
self.max_mana = mana
def level_up(self):
super().level_up() # 基本のレベルアップ処理
self.max_mana += 10 # 魔法使い特有の成長
self.mana = self.max_mana
print(f"マナが{self.max_mana}に増加した")
warrior = Warrior("戦士", 100, 20, 5)
mage = Mage("魔法使い", 80, 25, 50)
warrior.take_damage(10) # 戦士が6ダメージを受けた(5軽減)
warrior.level_up()
# 戦士がレベル2になった
# 防御力が7に上がった
3. 銀行口座システム
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
self.transaction_history = []
def deposit(self, amount):
self.balance += amount
self._record_transaction("入金", amount)
print(f"{amount}円入金しました")
def withdraw(self, amount):
if self.balance >= amount:
self.balance -= amount
self._record_transaction("出金", amount)
print(f"{amount}円出金しました")
return True
print("残高不足です")
return False
def _record_transaction(self, type, amount):
self.transaction_history.append(f"{type}: {amount}円")
class SavingsAccount(BankAccount):
def __init__(self, owner, balance=0, interest_rate=0.01):
super().__init__(owner, balance)
self.interest_rate = interest_rate
def deposit(self, amount):
super().deposit(amount) # 基本の入金処理
print("普通預金に入金されました")
def add_interest(self):
interest = self.balance * self.interest_rate
super().deposit(interest) # 親クラスのdepositで利息を入金
print(f"利息{interest}円が付与されました")
class CheckingAccount(BankAccount):
def __init__(self, owner, balance=0, overdraft_limit=10000):
super().__init__(owner, balance)
self.overdraft_limit = overdraft_limit
def withdraw(self, amount):
# 当座貸越を考慮した出金処理
if self.balance + self.overdraft_limit >= amount:
self.balance -= amount
self._record_transaction("出金", amount)
print(f"{amount}円出金しました")
return True
print("引き出し限度額を超えています")
return False
savings = SavingsAccount("田中", 100000)
savings.add_interest()
# 1000.0円入金しました
# 利息1000.0円が付与されました
checking = CheckingAccount("佐藤", 5000)
checking.withdraw(10000) # 5000円出金しました(当座貸越利用)
メソッド解決順序(MRO)とsuper()
多重継承でのsuper()
class A:
def method(self):
print("Aのメソッド")
class B(A):
def method(self):
print("Bのメソッド開始")
super().method()
print("Bのメソッド終了")
class C(A):
def method(self):
print("Cのメソッド開始")
super().method()
print("Cのメソッド終了")
class D(B, C):
def method(self):
print("Dのメソッド開始")
super().method()
print("Dのメソッド終了")
d = D()
d.method()
# Dのメソッド開始
# Bのメソッド開始
# Cのメソッド開始
# Aのメソッド
# Cのメソッド終了
# Bのメソッド終了
# Dのメソッド終了
print(D.__mro__) # メソッド解決順序を確認
super()の高度な使い方
1. 引数付きのsuper()呼び出し
class Base:
def greet(self, message):
print(f"Base: {message}")
class Child(Base):
def greet(self, message):
# 引数を渡してsuper()を呼び出し
super().greet(f"拡張された{message}")
child = Child()
child.greet("こんにちは") # Base: 拡張されたこんにちは
2. 戻り値を使ったsuper()
class Calculator:
def calculate(self, a, b):
return a + b
class AdvancedCalculator(Calculator):
def calculate(self, a, b):
result = super().calculate(a, b) # 親クラスの結果を取得
print(f"計算結果: {result}")
return result * 2 # 結果を2倍にして返す
calc = AdvancedCalculator()
result = calc.calculate(3, 5) # 計算結果: 8
print(result) # 16
3. プロパティでのsuper()
class Person:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name
class Student(Person):
def __init__(self, name, student_id):
super().__init__(name)
self.student_id = student_id
@property
def name(self):
base_name = super().name # 親クラスのプロパティを取得
return f"学生: {base_name}"
student = Student("田中", "S001")
print(student.name) # 学生: 田中
よくある間違いと注意点
1. super()の引数省略
# Python 3では引数を省略可能(推奨)
class ModernChild(Parent):
def method(self):
super().method() # ✅ 推奨
# Python 2の書き方(現在は不要)
class OldChild(Parent):
def method(self):
super(OldChild, self).method() # 古い書き方
2. super()とselfの混同
# ❌ 間違い
class Wrong(Parent):
def method(self):
self.method() # 無限再帰になる
# ✅ 正解
class Correct(Parent):
def method(self):
super().method() # 親クラスのメソッドを呼び出し
3. 初期化でのsuper()忘れ
# ❌ 問題のある例
class Child(Parent):
def __init__(self, child_arg):
# super().__init__()を忘れている
self.child_arg = child_arg
# ✅ 正しい例
class Child(Parent):
def __init__(self, parent_arg, child_arg):
super().__init__(parent_arg) # 親クラスの初期化
self.child_arg = child_arg
super()のベストプラクティス
1. 一貫したsuper()の使用
class GoodPractice(BaseClass):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) # 引数を適切に渡す
def method(self):
result = super().method() # 戻り値を活用
# 追加処理
return result
2. 協調的継承の実装
class Mixin1:
def process(self, *args, **kwargs):
print("Mixin1の処理")
super().process(*args, **kwargs) # 次のクラスに処理を委譲
class Mixin2:
def process(self, *args, **kwargs):
print("Mixin2の処理")
super().process(*args, **kwargs)
class Base:
def process(self, *args, **kwargs):
print("Baseの処理")
class Combined(Mixin1, Mixin2, Base):
def process(self, *args, **kwargs):
print("Combinedの処理開始")
super().process(*args, **kwargs)
print("Combinedの処理終了")
combined = Combined()
combined.process()
# Combinedの処理開始
# Mixin1の処理
# Mixin2の処理
# Baseの処理
# Combinedの処理終了
3. エラーハンドリングでのsuper()
class SafeChild(Parent):
def risky_method(self):
try:
result = super().risky_method()
return result
except Exception as e:
print(f"親クラスでエラー: {e}")
# エラーハンドリング処理
return None
まとめ
Pythonのsuper()は、クラス継承において親クラスの機能を効果的に活用するための重要な機能です。適切に使用することで、コードの再利用性と保守性を大幅に向上させることができます。
重要なポイント
- super()で親クラスのメソッドを呼び出す
- 初期化メソッドでは必ずsuper().init()を呼ぶ
- メソッドの拡張時に親クラスの処理を活用
- 多重継承ではMROを理解する
- 協調的継承でクラス間の連携を実現
まずは簡単な継承からsuper()の使い方を練習し、徐々に複雑なクラス階層でも適切にsuper()を活用できるようになりましょう。実際にコードを書いて練習することで、super()の概念がしっかりと身に付きます!
■プロンプトだけでオリジナルアプリを開発・公開してみた!!
■AI時代の第一歩!「AI駆動開発コース」はじめました!
テックジム東京本校で先行開始。
■テックジム東京本校
「武田塾」のプログラミング版といえば「テックジム」。
講義動画なし、教科書なし。「進捗管理とコーチング」で効率学習。
より早く、より安く、しかも対面型のプログラミングスクールです。
<短期講習>5日で5万円の「Pythonミニキャンプ」開催中。
<月1開催>放送作家による映像ディレクター養成講座
<オンライン無料>ゼロから始めるPython爆速講座


