class & self
class(ํด๋์ค)๋, ๋ฐ์ดํฐ์ ๊ธฐ๋ฅ์ด ํจ๊ป ๋ฌถ์ฌ ์๋ ํ๋์ ํ์ด๋ค. ์ด ํ์ ์ด์ฉํ์ฌ ์ฌ๋ฌ ๊ฐ์ object(๊ฐ์ฒด)๋ฅผ ๋ง๋ค ์ ์๋ค. ๊ฐ๊ฐ์ ๊ฐ์ฒด๋ ๋
๋ฆฝ์ ์ด์ด์ ์๋ก์๊ฒ ์ํฅ์ ์ฃผ์ง ์๋๋ค.
์ดํดํ๊ธฐ ์ฝ๊ฒ ์๋ฅผ ๋ค์ด ๋ณด์.
ํด๋์ค๋ ์ด์ฝ๋ฆฟ์ ๋ง๋๋ ํ๊ณผ ๊ฐ๋ค. ๋
น์ ์ด์ฝ๋ฆฟ์ ํ์ ๋ถ์ด ์ด์ฝ๋ฆฟ์ ๋ฌดํ์ ์์ฐํ ์ ์๋ค. ์ด ํ๋ก ๋ง๋ค์ด์ง ๊ฐ๊ฐ์ ์ด์ฝ๋ฆฟ์ ๊ฐ์ฒด๋ผ๊ณ ์๊ฐํ๋ฉด ๋๋ค.
๊ฐ๊ฐ์ ์ด์ฝ๋ฆฟ์ ๋
๋ฆฝ์ ์ด๋ฏ๋ก ์ด์ฝ๋ฆฟ์ ์ด๋ค ๋ฐ์ฝ๋ ์ด์
์ ์ฌ๋ฆฌ๋๋ผ๋ ๋ค๋ฅธ ์ด์ฝ๋ฆฟ์๋ ์ํฅ์ ์ฃผ์ง ์๋๋ค.
ํด๋์ค๋ฅผ ์ด์ฉํ๋ฉด ๊ฐ์ ๊ธฐ๋ฅ์ ํ๋ ๋
๋ฆฝ์ ์ธ ์ฌ๋ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์๊ธฐ ๋๋ฌธ์ ํธ๋ฆฌํ๋ค. ์ผ๊ธฐ์ฅ ํ๋ก๊ทธ๋จ์ ์์ฑํด์ผ ํ๋๋ฐ, ์ฌ๋๋ง๋ค ๋ค๋ฅธ ์ผ๊ธฐ์ฅ์ ๋ง๋ค์ด์ค ๋ ์ด๊ฑธ ์ผ์ผ์ด ์ฝ๋๋ก ์์ฑํ๋ฉด ์๊ฐ๋ญ๋น, ๊ทธ๋ฆฌ๊ณ ์์๋ญ๋น(!)์ด๊ธฐ ๋๋ฌธ์ด๋ค.
ํด๋์ค์ ๊ฐ์ฒด๋ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ ์ ์๋ค.
class chocoMaker:
pass
a = chocoMaker() # a ๊ฐ์ฒด ์์ฑ
b = chocoMaker() # b ๊ฐ์ฒด ์์ฑ
์์ง chocoMaker๋ผ๋ ํด๋์ค์ ์๋ฌด ๊ฐ์ด๋ ๊ธฐ๋ฅ์ด ํฌํจ๋์ด์์ง ์๋ค.
ํด๋์ค์ ํจ์๋ฅผ ํฌํจ์ํค๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
class chocoMaker:
def decorate(self, color, type):
self.color = color
self.type = type
a = chocoMaker()
a.decorate('white', 'sprinkle')
ํด๋์ค ๋ด๋ถ์ ํจ์๋ method(๋ฉ์๋)๋ผ๊ณ ๋ถ๋ฅธ๋ค.
๋ฉ์๋์ ์ฒซ ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ ๋๊ฐ self๋ฅผ ์ด๋ค. self์ ์ญํ ์ ๊ฐ์ฒด ์๊ธฐ ์์ ์ ํธ์ถํ๋ ๊ฒ์ด๋ค.
๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ๋๋ ์์ ๊ฐ์ด ๊ฐ์ฒด.๋ฉ์๋(...) ์ด๋ฐ ์์ผ๋ก ์์ฑํด์ผ ํ๋ค. ๊ทธ๋ฐ๋ฐ ์ฒซ ๋ฒ์งธ ๋งค๊ฐ๋ณ์์๋ self๋ ์ค์ ๋ก ์ฌ์ฉํ ๋ ์
๋ ฅํ์ง ์์๋ ๋๋ค. ๊ฐ์ฒด.๋ฉ์๋(...)์์ ๊ฐ์ฒด๊ฐ self๋ก ์๋์ผ๋ก ์ ๋ฌ๋๊ธฐ ๋๋ฌธ์ด๋ค.
๋ง์ฝ ์ด ๋ฐฉ๋ฒ์ด ๋ง์์ ๋ค์ง ์๋๋ค๋ฉด ์๋์ ๊ฐ์ด ์์ฑํด๋ ๋๋ค.
a = chocoMaker()
chocoMaker.decorate(a, 'white', 'sprinkle')
decorate ๋ฉ์๋์ ์ํ๋ฌธ์ ์ดํด๋ณด์.
a.decorate('white', 'sprinkle')๊ณผ ๊ฐ์ด ์์ฑํ๋ฉด, a๋ผ๋ ๊ฐ์ฒด ๋ด๋ถ์ color, type ๊ฐ์ฒด๋ณ์๊ฐ ์์ฑ๋๊ณ , 'white', 'sprinkle'๊ฐ์ด ์ ์ฅ๋๋ค.
ํด๋์ค๋ก ๋ง๋ ์ฌ๋ฌ ๊ฐ์ง ๊ฐ์ฒด ๊ฐ์ ๋
๋ฆฝ์ฑ์ด ๋ณด์ฅ๋๋ฏ๋ก, ํ ๊ฐ์ฒด์ ๊ฐ์ฒด๋ณ์์ ๊ฐ์ ์์ ํ๋ค๊ณ ํ์ฌ ๋ค๋ฅธ ๊ฐ์ฒด์ ๊ฐ์ฒด๋ณ์ ๊ฐ์ด ๋ฌ๋ผ์ง์ง ์๋๋ค.
init/์์ฑ์
chocoMaker ํด๋์ค์ ๋ค๋ฅธ ๋ฉ์๋๋ ์ถ๊ฐํด๋ณด์.
class chocoMaker:
def decorate(self, color, type):
self.color = color
self.type = type
def plate(self):
res = self.color + self.type
return res
plate ๋ฉ์๋๋ฅผ ์ถ๊ฐํ์๋ค. ์ด ์ํฉ์์ ๋ค์๊ณผ ๊ฐ์ด ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
b = chocoMaker()
b.plate()
์ด์ ๋ ์์ ๋ง๋ค์ด๋์๋ decorate ๋ฉ์๋๋ฅผ ๊ฑฐ์น์ง ์๊ณ ๋ plate ๋ฉ์๋๋ฅผ ์คํํ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค.
ํ์ง๋ง decorate ์ํ ํ plate๋ฅผ ์ํํ๋๋ก ํ๋ฉด ๋ถํธํ๋ค. ํ ๋ฒ์ ํด๊ฒฐํ ์ ์๋ ๋ฐฉ๋ฒ์ด ์์๊น?
์ฐ๋ฆฌ๋ ์ด๋ด ๋ ์์ฑ์(constructor)๋ฅผ ์ฌ์ฉํ๋ค. ์์ฑ์๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ์๋์ผ๋ก ํธ์ถ๋๋ ๋ฉ์๋๋ฅผ ์๋ฏธํ๋ค. __init__์ผ๋ก ์ฌ์ฉํ ์ ์๋ค.
๋ค์ ์ฝ๋๋ฅผ ์ดํด๋ณด์.
class chocoMaker:
def __init__(self, color, type):
self.color = color
self.type = type
def decorate(self, color, type):
self.color = color
self.type = type
def plate(self):
res = self.color + self.type
return res
c = chocoMaker('pink', 'decopen')
c๋ผ๋ ๊ฐ์ฒด๋ฅผ ๋ง๋ค ๋, decorate๋ฅผ ๋ถ๋ฌ์ฌ ํ์ ์์ด chocoMaker์ ํจ๊ป ์ํ๋ ๊ฐ์ ์ ๋ฌํ ์ ์๋ค.
์์
์ฝ๋๋ฅผ ์ง๋ค ๋ณด๋ฉด ๊ฐ์ ์์ฑ์ ๊ฐ๋ ์๋ก์ด ํด๋์ค๋ฅผ ์์ฑํด์ผ ํ ํ์๊ฐ ์๋ค.
์๋ฅผ ๋ค์ด, ์๊น๊ณผ ๋ฐ์ฝ๋ ์ด์
์ ์ข
๋ฅ๋ง์ ๊ฐ๋ chocoMaker ํด๋์ค์์ ๋ฐ์ฝ๋ ์ด์
์ ๊ฐ์๋ฅผ ์ถ๊ฐํ ํด๋์ค๋ฅผ ์๋ก ๋ง๋๋ ๊ฒฝ์ฐ๋ฅผ ์๊ฐํด๋ณด์.
์๋ก์ด ํด๋์ค๋ฅผ ์์ฑํ๋ ๊ฒ๋ณด๋ค ๊ธฐ์กด์ chocoMaker ํด๋์ค๋ฅผ ์ด์ฉํ์ฌ ๋ง๋๋ ํธ์ด ํจ์ฌ ๊ฐ๋จํ๋ค. ์ด๋ด ๋ ์ด์ฉํ๋ ๊ฒ์ด super().__init__๋ฅผ ์ด์ฉํ ์์์ด๋ค.
class superChocoMaker(chocoMaker):
def __init__(self, color, type, num): # num ์ถ๊ฐ
super().__init__(color, type)
self.num = num
def superPlate(self): # ๊ธฐ์กด ํด๋์ค์ ๋ฉ์๋๋ฅผ ์์
super().plate()
ํด๋์ค๋ฅผ ์์ฑํ ๋ ํด๋์ค ์ ๊ดํธ ์์ ์์ํ ํด๋์ค์ ์ด๋ฆ์ ์ ์ด๋ ๋ค์, super().__init__์ input์ผ๋ก ์์ํ ๊ธฐ์กด ํด๋์ค์ ์์์ ์ด๋ฆ์ ์ ์ด์ค๋ค.
๋๋ก๋ ์๋ก ๋ง๋ ํด๋์ค์์ ๊ธฐ์กด์ ๋ฉ์๋์ ๊ฐ์ ์ด๋ฆ์ ์ฌ์ฉํ์ง๋ง ๋ค๋ฅธ ๊ธฐ๋ฅ์ ํ๋ ๋ฉ์๋๋ฅผ ๋ง๋ค๊ณ ์ถ์ ์ ์๋ค. ๊ทธ๋ด ๋์๋ ๊ทธ๋ฅ ์์๋ฐ์ (์๋ก ๋ง๋ ) ํด๋์ค ๋ด๋ถ์์ ๋ฉ์๋๋ฅผ ๋ค์ ์ ์ํ๋ฉด ๋๋ค.
๊ธฐ์กด ํด๋์ค์ ๋ฉ์๋๋ฅผ ์์๋ฐ๊ณ ์ถ์ ๋๋ super().๋ฉ์๋์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์์ฑํ๋ค.