์ฝ๋ - ํจ์บ ์์ ์ฝ๋ ์ฐธ๊ณ (ํจ์บ ์์ ์ ๋ฆฌ)
<์ด์ ๊ธ>
https://silvercoding.tistory.com/39
[python ๊ธฐ์ด] 7. ํจ์ ๋ฐ ๋๋ค (lambda)
์ฝ๋ - ํจ์บ ์์ ์ฝ๋ ์ฐธ๊ณ (ํจ์บ ์์ ์ ๋ฆฌ) <์ด์ ๊ธ> https://silvercoding.tistory.com/38 https://silvercoding.tistory.com/37 https://silvercoding.tistory.com/36 https://silvercoding.tistory.com/35..
silvercoding.tistory.com
- ๋ณธ ํฌ์คํ ์์ ๋ค๋ฃฐ ๋ด์ฉ ์์ฝ
* ํด๋์ค ๋ณ์ & ์ธ์คํด์ค ๋ณ์ ์ฐจ์ด (์ค์)
* ํด๋์ค ๋ณ์ : ์ง์ ์ฌ์ฉ ๊ฐ๋ฅ, ๊ฐ์ฒด๋ณด๋ค ๋จผ์ ์์ฑ
* ์ธ์คํด์ค ๋ณ์ : ๊ฐ์ฒด ๋ง๋ค ๋ณ๋๋ก ์กด์ฌ, ์ธ์คํด์ค ์์ฑ ํ ์ฌ์ฉ
* ๋ค์์คํ์ด์ค : ๊ฐ์ฒด๋ฅผ ์ธ์คํด์คํ ํ ๋ ์ ์ฅ๋ ๊ณต๊ฐ ( (ex)์ด๋ฆ, ์ฃผ์, .... -> ๋ ๋ฆฝ์ ์ธ ๊ณต๊ฐ )
* ํด๋์ค ์์๊ณผ ๋ค์ค ์์
* ํด๋์ค ์ ์ธ
class ํด๋์ค๋ช :
ํจ์
ํจ์
ํจ์
[ํด๋์ค ๊ธฐ๋ณธ ์ฌ์ฉ]
- ์์ 1
class UserInfo:
# ์์ฑ, ๋ฉ์๋
def __init__(self, name):
self.name = name
print("์ด๊ธฐํ", name)
def user_info_p(self):
print('Name : ', self.name)
์ด๊ธฐ์ name์ ๋ฐ๋ ํด๋์ค UserInfo๋ฅผ ์ ์ธํ๋ค. user_info_p๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด self.name์ด ์ถ๋ ฅ๋๋ค.
user1 = UserInfo('Silver')
print(user1.name)
user1.user_info_p()
์ด๊ธฐํ Silver
Silver
Name : Silver
user2 = UserInfo('Park')
print(user1.name)
user2.user_info_p()
์ด๊ธฐํ Park
Park
Name : Park
print(id(user1))
print(id(user2))
2535817860096
2535817860544
๊ฐ์ ํด๋์ค์์ ๋์์ง๋ง user1๊ณผ user2 ๋ ์๋ก ๋ค๋ฅด๋ค. id๊ฐ์ด ๋ค๋ฅด๋ค๋ ๊ฒ์ผ๋ก ์ฆ๋ช ํ ์ ์๋ค.
* ๋ค์์คํ์ด์ค ํ์ธ
print(user1.__dict__)
print(user2.__dict__)
{'name': 'Silver'}
{'name': 'Park'}
__dict__ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ธ์คํด์ค์ ๋ค์์คํ์ด์ค๋ฅผ ํ์ธํ ์ ์๋ค. ์ ์ฅ๋ ๋ณ์์ ๊ฐ์ด key, valueํ์์ธ dictionary๋ก ์ถ๋ ฅ๋๋ค.
- ์์ 2
* self ์ ์ดํด (ํด๋์ค ๋ฉ์๋ vs ์ธ์คํด์ค ๋ฉ์๋)
<์ฐธ๊ณ ํฌ์คํ >
https://silvercoding.tistory.com/20?category=957523
[python ์ฌํ] 3. ํด๋์ค ๋ฉ์๋, ์ธ์คํด์ค ๋ฉ์๋, ์คํ ์ดํฑ ๋ฉ์๋
์ฝ๋ - ํจ์บ ์์ ์ฝ๋ ์ฐธ๊ณ (ํจ์บ ์์ ์ ๋ฆฌ) <์ด์ ๊ธ> https://silvercoding.tistory.com/19 https://silvercoding.tistory.com/18 [python ์ฌํ] 1. ๊ฐ์ฒด์งํฅ(OOP), ํด๋์ค ๊ธฐ์ด ์ฌ์ฉ ์ฝ๋ - ํจ์บ ์์ ์ฝ๋..
silvercoding.tistory.com
class SelfTest():
def function1():
print('function1 called ! ')
def function2(self):
print(id(self))
print('function2 called ! ')
๋ฉ์๋๋ฅผ 2๊ฐ ์์ฑํด ์ค๋ค. functionn1์ self๋ฅผ ์ธ์๋ก ๋ฐ์ง ์๊ณ , function2๋ self๋ฅผ ์ธ์๋ก ๋ฐ๋๋ค. ์ด ๊ฒฝ์ฐ์ ๊ฐ ๋๊ฐ์ ๋ฉ์๋์ ์ด๋ป๊ฒ ์ ๊ทผํ๋์ง ์คํํด ๋ณธ๋ค.
self_test = SelfTest()
self_test.function1()
TypeError: function1() takes 0 positional arguments but 1 was given
function1 ํจ์์ ์ธ์คํด์ค๋ก ์ ๊ทผํ๋๋ TypeError๊ฐ ๋ฐ์ํ๊ฒ ๋๋ค. self๋ฅผ ์ธ์๋ก ๋ฐ์ง ์๊ธฐ ๋๋ฌธ์ ํด๋์ค ๋ฉ์๋์ด๊ณ , ๋๊ตฌ(์ด๋ค ์ธ์คํด์ค)์ function1 ํจ์์ธ์ง ๋ชจ๋ฅด๋ ๊ฒ์ด๋ค.
SelfTest.function1() # ํด๋์ค ๋ฉ์๋
function1 called !
๋ฐ๋ผ์ ์ด๋ ๊ฒ ํด๋์ค ์์ฒด๋ก ์ ๊ทผํด ์ฃผ์ด์ผ ํธ์ถ์ด ๊ฐ๋ฅํ๋ค.
self_test.function2() # ์ธ์คํด์ค ๋ฉ์๋
print(id(self_test))
1805035034104
function2 called !
1805035034104
๋ฐ๋ฉด self๋ฅผ ์ธ์๋ก ๋ฐ์๋ function2 ๋ฉ์๋๋ ์ธ์คํด์ค๋ฅผ ํตํด ํธ์ถ ๊ฐ๋ฅํ๋ค. self์ id๋ฅผ ์ถ๋ ฅํ๋๋ก ์์ฑ ํ์๋๋ฐ, self_test ์ธ์คํด์ค์ id๊ฐ์ด ๊ฐ์ ๊ฒ์ ๋ณผ ์ ์๋ค. ๋ฐ๋ผ์ self๋ฅผ ์ธ์๋ก ๋ฐ๋ ํด๋์ค์ ๋ฉ์๋์ ์ธ์์ธ self์๋ ์ธ์คํด์ค๊ฐ ๋ค์ด๊ฐ๊ฒ ๋๋ ๊ฒ์ด๋ค.
SelfTest.function2() # -> ์์ธ
TypeError: function2() missing 1 required positional argument: 'self'
์ด์ฒ๋ผ ์ธ์คํด์ค๊ฐ ์๋ ํด๋์ค ์์ฒด๋ก ์ ๊ทผํ๋ฉด ๋๊ฐ์ด TypeError๊ฐ ๋๊ฒ ๋๋ค. ์ค๋ฅ ๋ด์ฉ์ ๋ฐ๋ฅด๋ฉด ์ธ์คํด์ค๋ฅผ ์ธ์์ ๋ฃ์ด์ฃผ๋ฉด ์คํ๋ ๊ฒ์ด๋ค.
SelfTest.function2(self_test) # ์ธ์์ ์ธ์คํด์ค ๋ฃ์ด์ฃผ๋ฉด ํด๋์ค์์ฒด๋ก ํธ์ถ ๊ฐ๋ฅ
2385469756864
function2 called !
- ์์ 3
: ํด๋์ค ๋ณ์ vs ์ธ์คํด์ค ๋ณ์
<์ฐธ๊ณ ํฌ์คํ >
https://silvercoding.tistory.com/19?category=957523
[python ์ฌํ] 2. ํด๋์ค ๊ธฐ์ด, ํด๋์ค ๋ณ์ , ์ธ์คํด์ค ๋ณ์
์ฝ๋ - ํจ์บ ์์ ์ฝ๋ ์ฐธ๊ณ (ํจ์บ ์์ ์ ๋ฆฌ) <์ด์ ๊ธ> https://silvercoding.tistory.com/18 [python ์ฌํ] 1. ๊ฐ์ฒด์งํฅ(OOP), ํด๋์ค ๊ธฐ์ด ์ฌ์ฉ ์ฝ๋ - ํจ์บ ์์ ์ฝ๋ ์ฐธ๊ณ 1. ๊ฐ์ฒด์งํฅ vs ์ ์ฐจ์งํฅ ๊ฐ๋จํ..
silvercoding.tistory.com
class WareHouse:
# ํด๋์ค ๋ณ์
stock_num = 0
def __init__(self, name):
# ์ธ์คํด์ค ๋ณ์
self.name = name
WareHouse.stock_num += 1
def __del__ (self):
WareHouse.stock_num -= 1
ํด๋์ค ๋ณ์์ ์ธ์คํด์ค ๋ณ์๋ฅผ ๊ฐ๊ฐ ์์ฑํด ์ฃผ์๋ค.
user1 = WareHouse('Kim')
user2 = WareHouse("Park")
user3 = WareHouse("Lee")
์ธ์คํด์ค๋ฅผ 3๊ฐ ๋ง๋ค์ด ์ค๋ค.
* ์ธ์คํด์ค ๋ค์์คํ์ด์ค, ํด๋์ค ๋ณ์ (๋ชจ๋ ์ธ์คํด์ค์ ๊ณต์ )
print(user1.__dict__)
print(user2.__dict__)
print(user3.__dict__)
{'name': 'Kim'}
{'name': 'Park'}
{'name': 'Lee'}
์์ ๊ฐ์ด ์ธ์คํด์ค์ ๋ค์์คํ์ด์ค๋ฅผ ์ถ๋ ฅํ๋ฉด name์ด ์ ๋๋ก ๋์ค๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
print(user1.name)
print(user2.name)
print(user3.name)
Kim
Park
Lee
* ํด๋์ค ๋ค์์คํ์ด์ค, ํด๋์ค ๋ณ์ (๋ชจ๋ ์ธ์คํด์ค์ ๊ณต์ )
print(WareHouse.__dict__) # ---> ํด๋์ค ์์ฒด์ ๋ค์ ์คํ์ด์ค์์ ํด๋์ค๋ณ์ ํ์ธ ๊ฐ๋ฅ
{'__module__': '__main__', 'stock_num': 3, '__init__': <function WareHouse.__init__
at 0x0000022B69124510>, '__del__': <function WareHouse.__del__ at 0x0000022B69124598>, '__dict__': <attribute '__di ct__' of 'WareHouse' objects>, '__weakref__': <attribute '__weakref__' of 'WareHouse' objects>, '__doc__': None}
์ด๋ ๊ฒ ํด๋์ค ๋ณ์๋ ํด๋์ค์ ๋ค์์คํ์ด์ค๋ฅผ ์ฐ์ด๋ณด๋ฉด ํ์ธํ ์ ์๋ค.
print(user1.stock_num) # ์์ ์ ๋ค์์คํ์ด์ค์ ์์ผ๋ฉด ํด๋์ค์ ๋ค์์คํ์ด์ค์์ ์ฐพ๊ณ , ํด๋์ค์ ๋ค์์คํ์ด์ค์๋ ์์ผ๋ฉด ๊ทธ๋ ์๋ฌ๊ฐ ๋๋ค.
print(user2.stock_num)
print(user3.stock_num)
3
3
3
ํด๋์ค ๋ณ์๋ ์ธ์คํด์ค๋ฅผ ํตํ์ฌ ์ ๊ทผํ ์๋ ์๋ค. ์๊ธฐ ์์ ์ ์ธ์คํด์ค ๋ค์์คํ์ด์ค์ ์ฐพ๋ ๋ณ์๊ฐ ์์ผ๋ฉด ํด๋์ค์ ๋ค์์คํ์ด์ค์์ ์ฐพ๊ณ , ํด๋์ค์ ๋ค์์คํ์ด์ค์๋ ํด๋น ๋ณ์๊ฐ ์์ผ๋ฉด ๊ทธ๋ ์ค๋ฅ๊ฐ ๋๊ฒ ๋๋ ๊ฒ์ด๋ค.
del user1
del๋ก user1์ ์ง์ฐ๋ฉด, ์์ ์ ์ธํด ๋์ ๋๋ก sock_num ๋ -1์ด ๋ ๊ฒ์ด๋ค.
# print(user1.stock_num)
print(user2.stock_num)
print(user3.stock_num)
2
2
[ํด๋์ค ์์]
- ์์ 1
* Car : ๋ถ๋ชจ ํด๋์ค
class Car:
""""Parent Class"""
def __init__(self, tp, color):
self.type = tp
self.color = color
def show(self):
return 'Car Class "Show Method!"'
* BmwCar : ์์ ํด๋์ค 1
class BmwCar(Car):
"""Sub Class"""
def __init__(self, car_name, tp, color):
super().__init__(tp, color)
self.car_name = car_name
def show_model(self) -> None:
return "Your Car Name : %s" % self.car_name
* BenzCar : ์์ ํด๋์ค 2
class BenzCar(Car):
"""Sub Class"""
def __init__(self, car_name, tp, color):
super().__init__(tp, color)
self.car_name = car_name
def show_model(self) -> None:
return "Your Car Name : %s" % self.car_name
def show(self):
print(super().show())
return 'Car Info: %s %s %s' % (self.car_name, self.type, self.color)
* ์ฌ์ฉ
model1 = BmwCar('520d', 'sedan', 'red')
์ฐ์ ๊ฐ์ฒด๋ฅผ ์์ฑํด ์ค๋ค.
print(model1.color) # Super
print(model1.type) # Super
print(model1.car_name) # Sub
print(model1.show()) # Super
print(model1.show_model()) # Sub
print(model1.__dict__)
red
sedan
520d
Car Class "Show Method!"
Your Car Name : 520d
{'type': 'sedan', 'color': 'red', 'car_name': '520d'}
๋ถ๋ชจํด๋์ค์์ ์ ์ธํด ๋์๋ color, type๋ณ์์ show() ๋ฉ์๋๊ฐ ์ ์์ ์ผ๋ก ์ถ๋ ฅ๋ ๊ฒ์ ๋ณผ ์ ์๋ค. ์ธ์คํด์ค์ ๋ค์์คํ์ด์ค์๋ ๋ณ์๊ฐ ๋ค์ด ๊ฐ ์๋ค.
# Method Overriding (์ค๋ฒ๋ผ์ด๋ : ์ฌ๋ผํ๋ค)
model2 = BenzCar("220d", 'suv', 'black')
print(model2.show())
Car Class "Show Method!"
Car Info: 220d suv black
# Parent Method Call
model3 = BenzCar('350s', 'sedan', 'silver')
print(model3.show())
Car Class "Show Method!"
Car Info: 350s sedan silver
BenzCar ํด๋์ค์ show() ๋ฉ์๋์์ super.show()๋ฅผ ์ฌ์ฉํ์ฌ ๋ถ๋ชจํด๋์ค์ ๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉ ํ์๋ค. ๋ฐ๋ผ์ ๋ถ๋ชจ ํด๋์ค์์ ์ ์ธํ์๋ ๋ด์ฉ๋ ํจ๊ป ์ถ๋ ฅ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
# Inheritance Info (์์์ ๋ณด ๋ฆฌ์คํธ ํํ๋ก ๋์ด)
print(BmwCar.mro()) # ์์ ๊ด๊ณ๋ฅผ ๋ณด์ฌ์ค๋ค.
print(BenzCar.mro())
# ๋ชจ๋ ํด๋์ค๋ object๋ฅผ ์์๋ฐ๋๋ค.
[<class '__main__.BmwCar'>, <class '__main__.Car'>, <class 'object'>]
[<class '__main__.BenzCar'>, <class '__main__.Car'>, <class 'object'>]
mro ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์์ ๊ด๊ณ๋ฅผ ๋ฆฌ์คํธ๋ก ์ถ๋ ฅํด๋ณผ ์ ์๋ค. ๋ชจ๋ ํด๋์ค๋ object๋ฅผ ์์ ๋ฐ๋ ๊ฒ ๋ํ ์์๋๊ธฐ !
- ์์ 2 : ๋ค์ค ์์
class X:
pass
class Y:
pass
class Z:
pass
class A(X, Y):
pass
class B(Y, Z):
pass
class M(B, A, Z):
pass
์ฐ์ ํด๋์ค X, Y, Z๋ฅผ ์ ์ธํ๊ณ , A๋ X, Y๋ฅผ , B๋ Y, Z ๋ฅผ , M์ B, A, Z ๋ฅผ ๋ค์ค์์ ๋ฐ๋๋ค. ์ด ๋์ Inheritance Info๋ฅผ ์ถ๋ ฅํด ๋ณด์.
print(M.mro())
print(A.mro())
[<class '__main__.M'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.X'>, <class '__main__.Y'>, <class '__main__.Z'>, <class 'object'>]
[<class '__main__.A'>, <class '__main__.X'>, <class '__main__.Y'>, <class 'object'>]
์ด์ฒ๋ผ ๋ค์ค์์์ด ๊ฐ๋ฅํ์ง๋ง , ๋๋ฌด ๋ง์ด ์์ ๋ฐ๊ฒ ๋๋ฉด ์ฝ๋๋ฅผ ํด์ํ๊ธฐ ์ด๋ ต๊ฒ ๋ง๋ค์ด์ง ์๋ ์์ผ๋ฏ๋ก ์ฃผ์ํ์ฌ ์ฌ์ฉํ๋ ๊ฒ์ด ๊ถ์ฅ๋๋ค.
'ํ๋ก๊ทธ๋๋ฐ ์ธ์ด > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[python ๊ธฐ์ด] 10. ํ์ผ ์ฝ๊ธฐ , ์ฐ๊ธฐ (open) (0) | 2021.08.02 |
---|---|
[python ๊ธฐ์ด] 9. ๋ชจ๋๊ณผ ํจํค์ง (0) | 2021.08.02 |
[python ๊ธฐ์ด] 7. ํจ์ ๋ฐ ๋๋ค (lambda) (0) | 2021.07.31 |
[python ๊ธฐ์ด] 6. ํ์ด์ฌ ํ๋ฆ์ ์ด (๋ฐ๋ณต๋ฌธ) - for, while (0) | 2021.07.31 |
[python ๊ธฐ์ด] 5. ํ์ด์ฌ ํ๋ฆ์ ์ด (์ ์ด๋ฌธ) - ์กฐ๊ฑด๋ฌธ if (0) | 2021.07.31 |