μ½λ - ν¨μΊ μμ μ½λ μ°Έκ³ (ν¨μΊ μμ μ 리)
<μ΄μ κΈ>
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 |