νμ΄μ¬μμ μμμ ν ν΄λμ€μ μμ±κ³Ό λ©μλλ₯Ό λ€λ₯Έ ν΄λμ€μ μ λ¬νλ κΈ°λ₯μ μλ―Ένλ€. μμμ μ¬μ©νλ©΄ κΈ°μ‘΄μ μ½λλ₯Ό μ¬μ¬μ©νκ³ νμ₯ν μ μλ€. κΈ°λ³Έμ μΌλ‘ νμ΄μ¬μ λͺ¨λ ν΄λμ€λ objectλΌλ κΈ°λ³Έ ν΄λμ€λ‘λΆν° μμλ°λλ€.

μμμ κΈ°λ³Έ νμμ λ€μκ³Ό κ°λ€.
class Parent:
pass
class Child(Parent):
pass
class Animal:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food):
print(f'{self.name}μ΄(κ°) {food}λ₯Ό λ¨Ήμ΅λλ€.')
def sleep(self, hour):
print(f'{self.name}μ΄(κ°) {hour}μκ° λμ μ μ μ‘λλ€.')
animal = Animal('λλ¬Ό', 10)
animal.eat('λ¨Ήμ΄')
animal.sleep(8)
> λλ¬Όμ΄(κ°) λ¨Ήμ΄λ₯Ό λ¨Ήμ΅λλ€.
> λλ¬Όμ΄(κ°) 8μκ° λμ μ μ μ‘λλ€.
class Dog(Animal):
pass
# Animal ν΄λμ€λ₯Ό μμλ°μκΈ° λλ¬Έμ Animal ν΄λμ€μ μμ±μ λ§€κ°λ³μλ₯Ό μ λ¬ν΄μΌ ν¨
Rucy = Dog('μμ΄', 5)
Rucy.eat('μ¬λ£')
Rucy.sleep(12)
> μμ΄μ΄(κ°) μ¬λ£λ₯Ό λ¨Ήμ΅λλ€.
> μμ΄μ΄(κ°) 12μκ° λμ μ μ μ‘λλ€.
class Parent:
def __init__(self):
print('λΆλͺ¨ ν΄λμ€ μμ±μ νΈμΆ')
class Child(Parent):
def __init__(self):
print('Child ν΄λμ€ μμ±μ νΈμΆ')
super().__init__() # μμ λ°μμΌλ©΄ 무쑰건 super νΈμΆ
print('μμ ν΄λμ€ μμ±μ νΈμΆ')
child = Child()
> Child ν΄λμ€ μμ±μ νΈμΆ
> λΆλͺ¨ ν΄λμ€ μμ±μ νΈμΆ
> μμ ν΄λμ€ μμ±μ νΈμΆ
objectλ νμ΄μ¬μ λͺ¨λ ν΄λμ€μ κΈ°λ³Έ ν΄λμ€μ΄λ€. object ν΄λμ€λ νμ΄μ¬μμ λͺ¨λ κ°μ²΄μ κΈ°λ³Έμ μΈ λμκ³Ό νΉμ±μ μ μνλ€.
class MyClass:
pass
# μμ μ½λλ λ€μκ³Ό λμΌν©λλ€.
class MyClass(object):
pass
λ©μλ μ€λ²λΌμ΄λ©(Method Overriding)μ κ°μ²΄ μ§ν₯ νλ‘κ·Έλλ°μμ μ€μν κ°λ μ€ νλλ‘, μλΈ ν΄λμ€(μμ ν΄λμ€)μμ μνΌ ν΄λμ€(λΆλͺ¨ ν΄λμ€)μ λ©μλλ₯Ό μ¬μ μνλ κ²μ μλ―Ένλ€. μ€λ²λΌμ΄λ©μ μ¬μ©νλ©΄, μλΈ ν΄λμ€μμ μμλ°μ λ©μλμ λμμ λ³κ²½νκ±°λ νμ₯ν μ μλ€.
class Animal:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food):
print(f'{self.name}μ΄(κ°) {food}λ₯Ό λ¨Ήμ΅λλ€.')
def sleep(self, hour):
print(f'{self.name}μ΄(κ°) {hour}μκ° λμ μ μ μ‘λλ€.')
class Dog(Animal):
def run(self):
print(f'{self.name} λ¬λ¦½λλ€')
SSongi = Dog('μμ΄', 5)
SSongi.eat('μ¬λ£')
SSongi.sleep(8)
SSongi.run()
> μμ΄μ΄(κ°) μ¬λ£λ₯Ό λ¨Ήμ΅λλ€.
> μμ΄μ΄(κ°) 8μκ° λμ μ μ μ‘λλ€.
> μμ΄ λ¬λ¦½λλ€
class Animal:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food):
print(f'{self.name}μ΄(κ°) {food}λ₯Ό λ¨Ήμ΅λλ€.')
def sleep(self, hour):
print(f'{self.name}μ΄(κ°) {hour}μκ° λμ μ μ μ‘λλ€.')
class Dog(Animal):
def run(self):
print(f'{self.name} λ¬λ¦½λλ€')
def eat(self, food):
print(f'{self.name} {food}λ₯Ό μμ£Ό λ§μκ² λ¨Ήμ΅λλ€')
def superEat(self, food):
super().eat(food)
SSongi = Dog('μμ΄', 5)
SSongi.eat('μ¬λ£')
SSongi.superEat('μ¬λ£')
> μμ΄ μ¬λ£λ₯Ό μμ£Ό λ§μκ² λ¨Ήμ΅λλ€
> μμ΄μ΄(κ°) μ¬λ£λ₯Ό λ¨Ήμ΅λλ€.
animal = Animal('λλ¬Ό', 10)
animal.eat('λ¨Ήμ΄')
> λλ¬Όμ΄(κ°) λ¨Ήμ΄λ₯Ό λ¨Ήμ΅λλ€.
animal.run()
> AttributeError: 'Animal' object has no attribute 'run'
λ€μ€ μμμ ν΄λμ€κ° λ μ΄μμ λΆλͺ¨ ν΄λμ€λ‘λΆν° μμμ λ°λ κΈ°λ₯μ μλ―Ένλ€. νμ΄μ¬μ λ€λ₯Έ λ§μ κ°μ²΄ μ§ν₯ μΈμ΄μ λ¬λ¦¬ λ€μ€ μμμ μ§μνλ€. λ€μ€ μμκΈ μ¬μ©νλ©΄ μ½λμ μ¬μ¬μ©μ±μ ν₯μμν¬ μ μμ§λ§, λμμ 볡μ‘μ±μ΄ λμμ§κΈ° λλ¬Έμ μ£Όμν΄μΌ νλ€.
λ€μ€ μμμ κΈ°λ³Έ νμμ λ€μκ³Ό κ°λ€.
class Parent1:
pass
class Parent2:
pass
class Child(Parent1, Parent2):
pass
class Animal:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food):
print(f'{self.name} {food}λ₯Ό λ¨Ήμ΅λλ€.')
def sleep(self, hour):
print(f'{self.name} {hour}μκ° λμ μ μ μ‘λλ€.')
class Human:
def __init__(self, name, age):
self.name = name
self.age = age
def study(self, hour):
print(f'{self.name} {hour}μκ° λμ 곡λΆλ₯Ό ν©λλ€.')
def sleep(self, hour):
print(f'{self.name} {hour}μκ° λμ κΏμ μ μ‘λλ€.')
class YoominKong(Animal, Human):
pass
Kong = YoominKong('μ 민콩', 5)
Kong.eat('λ§λΌν')
Kong.study(2)
Kong.sleep(8) # Animalμ sleepλ₯Ό μμλ°μ.
> μ 민콩 λ§λΌνλ₯Ό λ¨Ήμ΅λλ€.
> μ 민콩 2μκ° λμ 곡λΆλ₯Ό ν©λλ€.
> μ 민콩 8μκ° λμ μ μ μ‘λλ€.
print(YoominKong.mro()) # λ©μλκ° κ²Ήμ³€μ λ μ΄λ μμλ‘ λΆλ¦¬λ μ§ μλ €μ€
> [<class '__main__.YoominKong'>, <class '__main__.Animal'>, <class '__main__.Human'>, <class 'object'>]
super()λ νμ΄μ¬μ λ΄μ₯ ν¨μλ‘μ, μμκ³Ό κ΄λ ¨λ μμ μ μνν λ μ¬μ©λλ€. νΉν, μμ ν΄λμ€μμ λΆλͺ¨ ν΄λμ€μ λ©μλλ₯Ό νΈμΆν λ μ¬μ©λλ€. super()μ μ£Όμ λͺ©μ μ μμ ν΄λμ€μμ λΆλͺ¨ ν΄λμ€μ λ©μλλ₯Ό μ€λ²λΌμ΄λ(μ¬μ μ)νλ©΄μλ κ·Έ μ€λ²λΌμ΄λλ λ©μλ λ΄μμ λΆλͺ¨ ν΄λμ€μ μλ³Έ λ©μλλ₯Ό νΈμΆνλ κ²μ΄λ€.
class Parent:
def hello(self):
print('λΆλͺ¨ ν΄λμ€μ hello λ©μλ')
class Child(Parent):
def hello(self):
super().hello()
print('μμ ν΄λμ€μ hello λ©μλ')
child = Child()
child.hello()
> λΆλͺ¨ ν΄λμ€μ hello λ©μλ
> μμ ν΄λμ€μ hello λ©μλ
class Parent:
def __init__(self, value):
self.value = value
class Child(Parent):
def __init__(self, value, child_value):
super().__init__(value)
self.child_value = child_value
child = Child(10, 20)
print(child.child_value) # 10μ super(Parent)λ‘ λ€μ΄κ°μ child_valueλ 20μ΄ λ¨
print(child.value)
> 20
> 10
parent = Parent(10)
print(parent.value)
> 10
class Base:
def hello(self):
print('Baseμ hello()')
print('Base ν΄λμ€μ hello() λ©μλ')
class A(Base):
def hello(self):
print('Aμ hello()')
super().hello()
print('A ν΄λμ€μ hello() λ©μλ')
class B(Base):
def hello(self):
print('Bμ hello()')
super().hello()
print('B ν΄λμ€μ hello() λ©μλ')
class Child(A, B):
def hello(self):
print('Childμ hello()')
super().hello()
print('Child ν΄λμ€μ hello() λ©μλ')
child = Child()
child.hello() # child μΆλ ₯λκ³ super λλ¬Έμ Aλ‘ κ°μ Aμ νλ¦°νΈ μ°νκ³ λ°μ super λλ¬Έμ Bλ‘ κ°κ³ λ Bμμ Base κΉμ§ κ° λ€μμ λ°λμμλλ‘ λ§μ§λ§μ€ printλ¬Έμ΄ μ°¨λ‘μ°¨λ‘ μΆλ ₯λ¨
> Childμ hello()
> Aμ hello()
> Bμ hello()
> Baseμ hello()
> Base ν΄λμ€μ hello() λ©μλ
> B ν΄λμ€μ hello() λ©μλ
> A ν΄λμ€μ hello() λ©μλ
> Child ν΄λμ€μ hello() λ©μλ
Child.mro() # μμ μΆλ ₯ κ²°κ³Όμ λμΌν μμ. μ¦ λ€μ€ μμμ mroμ μν μμλλ‘ μ§νλ¨. μ΄ μμλ μκ³ λ¦¬μ¦μ μν΄ μ ν΄μ§κΈ° λλ¬Έμ λ€μ€μμμ΄λ λ©μλ μ΄λ¦μ΄ κ²ΉμΉλ κ²½μ° mroλ₯Ό νμΈν΄λ³΄λ κ² μ’λ€.
> [__main__.Child, __main__.A, __main__.B, __main__.Base, object]