ํด๋์ค_Dog์์ & Self์์ ๊ด๋ จ
์๋
ํ์ธ์, ์ ๋ ์ ๋ง ์ด๋ณด์ด์ง๋ง, ํน์๋ ํด์ ๋ต๋ณ ๋ฌ์๋ด
๋๋ค.์ง๋ฌธํด์ฃผ์ Dog ํด๋์ค์์, a.species๋ ์๋์ ํ์
จ๋ค๋ ๊ฒ์ผ๋ก ์ ์ถํด๋ณด๋ฉด,Dog ํด๋์ค์ ์ธ์คํด์ค๋ช
์ด a ๋ผ๊ณ ์๊ฐ๋๋๋ฐ์.class Dog : species = 'firstdog' def __init__(self, name, age): self.name = name self.age = age์ ์ฝ๋์์, species๋ ํด๋์ค์ ๋ณ์(attribute)์ฌ์ ํด๋์ค์ ํด๋น๋๋ ๋ชจ๋ ์ธ์คํด์ค์ ์ ์ฉ๋ฉ๋๋ค.์ฆ, Dog ํด๋์ค์ ๊ณตํต์ ์ธ ํน์ง์ ๋ณ์ ์๋ฆฌ์ ์ ์ด์ฃผ์๋ฉด ๋ ๊ฒ ๊ฐ์ต๋๋ค.๋ฐ๋ผ์ a.species๋ฅผ ํ๋ฉด, 'firstdog'์ด ์ถ๋ ฅ๋ ๊ฒ ๊ฐ๊ตฌ์.class SelfTest(): def func1(): print('func1 called') def func2(self): print('func2 called')์ ์ฝ๋์์ func1๊ณผ 2์ ์ฐจ์ด๋ (self) ์ ์ฌ๋ถ, ์ฆ ์ธ์คํด์ค๋ฅผ ๋ฐ์ ์ ์๋ ์๋์ ์ฐจ์ด์
๋๋ค.self๋ ์ธ์คํด์ค(Dog ํด๋์ค ๊ฒฝ์ฐ์์ a)๋ฅผ ์๋ฏธํ๋๋ฐ, func1()์ ๊ฒฝ์ฐ self๊ฐ ์์ผ๋ฏ๋ก,b = SelfTest()๋ผ๊ณ ์ธ์คํด์ค๋ฅผ b๋ผ๊ณ ์ด๋ฆ์ง์์ ๋, b๊ฐ SelfTest ํด๋์ค์์ self์ ํด๋น๋๋ฏ๋ก, ์ธ์คํด์ค(self)๊ฐ ๋ค์ด๊ฐ ์ ์๋ func2๊ฐ ์คํ๋์ด 'func2 called'๊ฐ ์ถ๋ ฅ๋ ๊ฒ์
๋๋ค.func1์ ์คํํ์๋ ค๋ฉดSelfTest.func1()๋ผ๊ณ ์ฝ๋ฉํ์๋ฉด, ํด๋์ค์ ํจ์๋ก์ ์ง์ ์คํ๋ ๊ฒ์ด๊ณ , func2๋ฅผ ์คํํ์๋ ค๋ฉดSelfTest.func2(b) ๋๋ b.func2()๋ผ๊ณ ํธ์ถํ์๋ฉด ๋ ๊ฒ ๊ฐ์ต๋๋ค. ๋ต๋ณ์ด ๋์์์๋ค์. ํต์ฌ์ self = ์ธ์คํด์ค๋ค !! ์
๋๋ค.