7.1 super()
๋ฉ์๋
์์ ๊ณ์ธต์ ์๊ธฐํ๋ ๊น์, ๊ธฐ๋ณธ ํด๋์ค์ ํ๋์ ๋ฉ์๋๋ฅผ ๋ค๋ฃจ๋ ๊ฒ์ ๋ํ
์ธ๋ถ ์ฌํญ๋ค์ ๋
ผ์ํ๋ ๊ฒ์ด ์ ์ฉํ ๊ฑฐ์ผ. Python์๋ ์ด๊ฑธ ์ํด ํน๋ณํ
๋ฉ์๋ super()
๊ฐ ์์ด. ์ด ๋ฉ์๋๋ ์์ ํด๋์ค ๋ด์์ ๊ธฐ๋ณธ ํด๋์ค์ ๋ฉ์๋๋ฅผ
ํธ์ถํ ๋ ์ฌ์ฉ๋ผ.
์ด ๋ฉ์๋๋ ์ธ ๊ฐ์ง ์ฃผ์ ์ฌ์ฉ ์ฌ๋ก๊ฐ ์์ด:
๋ถ๋ชจ ํด๋์ค ๋ฉ์๋ ํธ์ถ:
super()
๋ฉ์๋๋ ๋ถ๋ชจ ํด๋์ค์ ๋ฉ์๋๋ฅผ ์์ ํด๋์ค์์ ํธ์ถํ ์ ์๊ฒ ํด ์ค,
๋ถ๋ชจ ํด๋์ค์ ์ด๋ฆ์ ๋ช
์์ ์ผ๋ก ์ง์ ํ ํ์๊ฐ ์์ด. ํนํ ๋ค์ค ์์์ ๋ค๋ฃฐ ๋ ์ ์ฉํ๊ณ ,
ํด๋์ค ๊ณ์ธต์ ๋ณ๊ฒฝํ ๋ ๋ฐ์ํ ์ ์๋ ์ค๋ฅ๋ฅผ ํผํ๋ ๋ฐ ๋์์ด ๋ผ.
๊ธฐ๋ณธ ํด๋์ค ์ด๊ธฐํ:
super()
๋ ์์ ํด๋์ค์ ์์ฑ์์์ ๊ธฐ๋ณธ ํด๋์ค์ ์์ฑ์๋ฅผ ํธ์ถํ ๋ ์์ฃผ
์ฌ์ฉ๋ผ, ์ด๋ ์์ ํด๋์ค ๋ด์์ ๊ธฐ๋ณธ ํด๋์ค์ ์์ฑ์ ์ด๊ธฐํํ ์ ์๊ฒ ํด ์ค.
๋ค์ค ์์ ์ง์:
๋ค์ค ์์์ ๊ฒฝ์ฐ
super()
๋ ๋ฉ์๋ ํธ์ถ์ ์์(MRO, Method Resolution Order)๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ํด๊ฒฐํด,
๋ถ๋ชจ ํด๋์ค์ ๋ฉ์๋๋ฅผ ๋ช
์์ ์ผ๋ก ํธ์ถํ๋ ๊ฒ๋ณด๋ค ์ด๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ ์ ํธ๋ผ. ์ด๊ฒ์ ๋ํด์๋
๋์ค์ ๋ ์๊ธฐํ ๊ฑฐ์ผ.
7.2 ๊ธฐ๋ณธ ํด๋์ค์ ์์ฑ์
๊ธฐ๋ณธ ํด๋์ค์ ์์ฑ์๋ ๋ช ์์ ์ผ๋ก ํธ์ถํด์ผ ํด. ์๋์ผ๋ก ๋ฐ์ํ ๊ฒ ๊ฐ์ง๋ง, ์ค์ ๋ก๋ ๊ทธ๋ ์ง ์์. ๊ธฐ๋ณธ ํด๋์ค์ ์์ฑ์๋ ํญ์ ๋ช ์์ ์ผ๋ก ๋ถ๋ฌ์ค๊ณ , ์ข ์ข ํน๋ณํ ์ธ์๋ฅผ ์ ๋ฌํด์ผ ํ ํ์๊ฐ ์๊ธฐ ๋๋ฌธ์ด์ผ.
์์:
class Animal:
def __init__(self, type, name):
self.type = type
self.name = name
class Dog(Animal):
def __init__(self, name):
super().__init__("๊ฐ", name) # ๊ธฐ๋ณธ ํด๋์ค ์์ฑ์ ํธ์ถ
class Cat(Animal):
def __init__(self, name):
super().__init__("๊ณ ์์ด", name) # ๊ธฐ๋ณธ ํด๋์ค ์์ฑ์ ํธ์ถ
# Dog ๊ฐ์ฒด ์์ฑ
dog = Dog("Buddy")
print(dog)
์ด ์์์์ ๊ธฐ๋ณธ ํด๋์ค (Animal)
์ ์์ฑ์๋ ๋ ๊ฐ์ ๋งค๊ฐ๋ณ์๋ฅผ ๊ฐ์ ธ: ๋๋ฌผ์ ํ์
๊ณผ ๊ทธ ์ด๋ฆ. ๊ทธ๋ฆฌ๊ณ ์์ ํด๋์ค๋ ํ๋๋ง ๊ฐ์ ธ โ ์ด๋ฆ.
๋ฐ๋ก Dog
์ Cat
ํด๋์ค์ ์์ฑ์์์ ๊ธฐ๋ณธ ํด๋์ค ์์ฑ์์ ์ ๋ฌํ ๊ฒ์ด ๊ฒฐ์ ๋ผ โ ๋๋ฌผ์ ํ์
์ด๋ฆ์ธ "๊ฐ"์ "๊ณ ์์ด".
๊ทธ๋์:
- ๊ธฐ๋ณธ ํด๋์ค์ ์์ฑ์๋ ์์ ํด๋์ค์ ์์ฑ์์์ ๋ฐ๋์ ํธ์ถํด์ผ ํด.
- ์ด๋ฅผ ์ํด
super()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ผ ํด. -
self
๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ๋ก ์ ๋ฌํ ํ์๋ ์์ด โ Python์ด ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ ์๋์ผ๋ก ๋์ ํด ์ค ๊ฑฐ์ผ.
7.3 super()
๋ฉ์๋ ์ฌ์ฉ
super()
๋ฉ์๋๋ Python์์ ์์ฑ์๋ฟ๋ง ์๋๋ผ ๋ถ๋ชจ ํด๋์ค์ ๋ฉ์๋๋ฅผ
ํธ์ถํ๊ธฐ ์ํด ํด๋์ค์ ๋ค๋ฅธ ๋ฉ์๋์์๋ ์ฌ์ฉํ ์ ์์ด. ๋ถ๋ชจ ํด๋์ค์ ์ ์๋ ๋ฉ์๋์
๋์์ ํ์ฅํ๊ฑฐ๋ ์์ ํด์ผ ํ ๋ ์ด๊ฒ ์ ์ฉํ ์ ์์ด.
๋ช ๊ฐ์ง ์์๋ฅผ ์ดํด๋ณด์:
์์ ๋ฉ์๋์์ ๋ถ๋ชจ ํด๋์ค์ ๋ฉ์๋ ํธ์ถ
์ด ์์์์ Dog
ํด๋์ค์ speak()
๋ฉ์๋๋ ๋จผ์ super()
๋ฅผ ํตํด
Animal
ํด๋์ค์ speak()
๋ฉ์๋๋ฅผ ํธ์ถํ ๋ค์์ ์์ฒด ํ๋์ ์ถ๊ฐํด.
class Animal:
def speak(self):
return "์ด๋ค ์ผ๋ฐ์ ์ธ ๋๋ฌผ์ ์๋ฆฌ"
class Dog(Animal):
def speak(self):
parent_speech = super().speak() # ๋ถ๋ชจ ํด๋์ค์ ๋ฉ์๋ ํธ์ถ
return f"{parent_speech} ๊ทธ๋ฆฌ๊ณ ๊ฐ๊ฐ ์ง์ด!"
dog = Dog()
print(dog.speak()) # ์ถ๋ ฅ: ์ด๋ค ์ผ๋ฐ์ ์ธ ๋๋ฌผ์ ์๋ฆฌ ๊ทธ๋ฆฌ๊ณ ๊ฐ๊ฐ ์ง์ด!
๋ถ๋ชจ ํด๋์ค์ ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ์ํ ํ์ธ
์ด ์์์์ Dog
ํด๋์ค์ check_health()
๋ฉ์๋๋ฅผ ํตํด
Animal
ํด๋์ค์ check_health()
๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ์ถ๊ฐ์ ์ธ ๊ฒ์ฌ๋ฅผ ์ํํด.
class Animal:
def check_health(self):
return "๋๋ฌผ์ด ๊ฑด๊ฐํด"
class Dog(Animal):
def check_health(self):
parent_check = super().check_health() # ๋ถ๋ชจ ํด๋์ค์ ๋ฉ์๋ ํธ์ถ
return f"{parent_check}. ๊ฐ๊ฐ ์ฐ์ฑ
์ด ํ์ํด!"
dog = Dog()
print(dog.check_health()) # ์ถ๋ ฅ: ๋๋ฌผ์ด ๊ฑด๊ฐํด. ๊ฐ๊ฐ ์ฐ์ฑ
์ด ํ์ํด!
์ํ๋ฅผ ๋ณ๊ฒฝํ๋ ๋ฉ์๋์์ ๋ถ๋ชจ ํด๋์ค์ ๋ฉ์๋ ํธ์ถ
์ด ์์์์ SavingsAccount
ํด๋์ค์ withdraw()
๋ฉ์๋๋ ๋จผ์
์ธ์ถ ํ๋๊ฐ ์ด๊ณผ๋์ง ์์๋์ง ํ์ธํ ๋ค์, ์ด๊ณผ๋์ง ์์๋ค๋ฉด BankAccount
ํด๋์ค์
withdraw()
๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ์์
์ ์ํํด.
class BankAccount:
def __init__(self, balance):
self.balance = balance
def withdraw(self, amount):
if self.balance >= amount:
self.balance -= amount
return f"{amount} ์ธ์ถํ์ด. ์๋ก์ด ์์ก: {self.balance}"
return "์์ก ๋ถ์กฑ"
class SavingsAccount(BankAccount):
def withdraw(self, amount):
if amount > 1000:
return "์ธ์ถ ํ๋ ์ด๊ณผ"
return super().withdraw(amount) # ๋ถ๋ชจ ํด๋์ค์ ๋ฉ์๋ ํธ์ถ
savings = SavingsAccount(1500)
print(savings.withdraw(500)) # ์ถ๋ ฅ: 500 ์ธ์ถํ์ด. ์๋ก์ด ์์ก: 1000
print(savings.withdraw(1500)) # ์ถ๋ ฅ: ์ธ์ถ ํ๋ ์ด๊ณผ
GO TO FULL VERSION