※ 다중 상속 상황에서 사용에 주의

class Unit:
    def __init__(self):
        print("Unit 생성자")
 
class Flyable:
    def __init__(self):
        print("Flyable 생성자")
 
class FlyableUnit(Unit, Flyable):
    def __init__(self):
        super().__init__()
 
# 드랍쉽 생성 : 공중 유닛, 공격 X
dropship = FlyableUnit()

출력결과는 다음과 같다.

Unit 생성자

즉, Unit 생성자는 init으로 초기화가 되었는데, Flyable 생성자는 초기화되지 않은 것이다.


class Unit:
    def __init__(self):
        print("Unit 생성자")
 
class Flyable:
    def __init__(self):
        print("Flyable 생성자")
 
class FlyableUnit(Flyable, Unit):
    def __init__(self):
        super().__init__()
 
# 드랍쉽 생성 : 공중 유닛, 공격 X
dropship = FlyableUnit()

출력결과는 다음과 같다.

Flyable 생성자

즉, 2개 이상의 다중 속성을 받는 생성자의 경우, super을 쓰면 먼저 쓰인 class만 상속된다. 따라서 다중 상속을 하거나 모든 부모 class의 초기화가 필요하다면 따로 초기화를 명시하는 것이 필요하다.


class Unit:
    def __init__(self):
        print("Unit 생성자")
 
class Flyable:
    def __init__(self):
        print("Flyable 생성자")
 
class FlyableUnit(Flyable, Unit):
    def __init__(self):
        Unit.__init__(self)
        Flyable.__init__(self)
 
# 드랍쉽 생성 : 공중 유닛, 공격 X
dropship = FlyableUnit()

출력결과는 다음과 같다.

Unit 생성자
Flyable 생성자