μ½λ - ν¨μΊ μμ μ½λ μ°Έκ³ (ν¨μΊ μμ μ 리)
<μ΄μ κΈ>
https://silvercoding.tistory.com/26
[python μ¬ν] 9. λ³μ λ²μ, Closure, Decorator
μ½λ - ν¨μΊ μμ μ½λ μ°Έκ³ (ν¨μΊ μμ μ 리) <μ΄μ κΈ> https://silvercoding.tistory.com/25 μ΄λ² ν¬μ€νΈμμλ λ°μ½λ μ΄ν°λ₯Ό μμλ³Ό κ²μ΄λ€. λ°μ½λ μ΄ν°λ μ΄λ ΅λ€κ³ λλΌλ κ²μ΄ λΉμ°νλ€. λ°μ½λ μ΄ν°
silvercoding.tistory.com
- μμ λ³΅μ¬ & is vs __eq__ (==)
x = {'name': 'Kim','age': 33, 'city' : 'Seoul'}
y = x # ---> μμ 볡μ¬
μ°μ xμ λμ λ리λ₯Ό μ μ₯νλ€. κ·Έλ€μ λ¨μν 볡μ¬λ₯Ό νλ€.
print('EX2-1', id(x), id(y))
print('EX2-2 - ', x == y)
print('EX2-3 - ', x is y)
print('EX2-4 - ', x, y)
EX2-1 2324412065760 2324412065760
μ°μ id κ°μ μΆλ ₯ν΄λ³΄λ λ κ°μ΄ μμ ν κ°μλ€.
EX2-2 - True
EX2-3 - True
2-2μ 2-3μ μ§κ΄μ μΌλ‘ λ΄λ xμ yκ° κ°μμ§λ₯Ό λ¬Όμ΄λ³΄λ μ½λμΈ κ² κ°λ€. νμ§λ§ λ€λ₯Έ μ’ λ₯μ νμΈλ²μ΄λ€. __eq__ (==) μ κ°μ κ²½μ°λ xμ yμ κ°μ΄ κ°μμ§λ₯Ό νμΈνλ κ²μ΄κ³ , isλ xμ yμ idκ°μ΄ κ°μμ§λ₯Ό νμΈνλ κ²μ΄λ€.
** μ 리
==(__eq__) : κ°μ λΉκ΅ vs is : κ°μ²΄ μ£Όμ (μ 체μ±) λΉκ΅
EX2-4 - {'name': 'Kim', 'age': 33, 'city': 'Seoul'} {'name': 'Kim', 'age': 33, 'city': 'Seoul'}
μ μμ μμλ κ°, id λͺ¨λ κ°λ€. xμ yλ₯Ό μΆλ ₯νλ©΄ λΉμ°ν κ²°κ³Όλ‘ κ°μ λμ λλ¦¬κ° λμ€λ κ²μ λ³Ό μ μλ€.
xμ κ°μ μμ νκ±°λ μΆκ°νλ©΄ yλ λ°λκΉ? idκ°μ΄ κ°μΌλ―λ‘ λ°λ κ²μΌλ‘ μΆμ ν μ μλ€.
x['class'] = 10
print('EX2-5 - ', x, y)
EX2-5 - {'name': 'Kim', 'age': 33, 'city': 'Seoul', 'class': 10} {'name': 'Kim', 'age': 33, 'city': 'Seoul', 'class': 10}
xμ κ°μ μΆκ°νλλ yλ λκ°μ΄ μΆκ°λλ κ²μ μ μ μλ€.
z = {'name': 'Kim','age': 33, 'city' : 'Seoul', 'class' : 10}
μ λμ λ리 κ°μ²΄λ₯Ό λ§λ€μ΄ λ³Έλ€. κ°μ xμ κ°λλ‘!
print('EX2-6 - ', x, z)
print('EX2-7 - ', x is z) # κ°μ κ°μ²΄λ
print('EX2-8 - ', x is not z)
print('EX2-9 - ', x == z) # κ°μ΄ κ°λ
EX2-6 - {'name': 'Kim', 'age': 33, 'city': 'Seoul', 'class': 10} {'name': 'Kim', 'age': 33, 'city': 'Seoul', 'class': 10}
EX2-7 - False
EX2-8 - True
EX2-9 - True
xμ zλ₯Ό λΉκ΅ν΄ 보면 idκ°μ λ€λ₯΄κ³ , κ°λ§ κ°λ€κ³ λμ€λ κ²μ λ³Ό μ μλ€. μ΄ κ²½μ°μλ xμ κ°μ λ³κ²½νμ¬λ zμ κ°μ λ³νμ§ μμ κ²μ΄λ€. (λΉμ°ν μ리)
*** cf ) νν (λΆλ³ν) λΉκ΅
tuple1 = (10, 15, [100, 1000])
tuple2 = (10, 15, [100, 1000])
print('EX3-1 - ', id(tuple1), id(tuple2))
print('EX3-2 - ', tuple1 is tuple2)
print('EX3-3 - ', tuple1 == tuple2)
print('EX3-4 - ', tuple1.__eq__(tuple2))
EX3-1 - 2324415315832 2324415315544
EX3-2 - False
EX3-3 - True
EX3-4 - True
- Copy & DeepCopy (μμ λ³΅μ¬ , κΉμ 볡μ¬)
tl1 = [10, [100, 105], (5, 10, 15)]
tl2 = tl1
tl3 = list(tl1)
리μ€νΈ μμ μ«μ, 리μ€νΈ, ννμ κ°κ° λ£μ ν, tl2μλ μμ 볡μ¬λ₯Ό , tl3μλ listμ μΈμμ λ£μ΄ μ κ°μ²΄λ₯Ό μμ±ν΄ μ€λ€.
print('EX4-1 - ', tl1 == tl2)
print('EX4-2 - ', tl1 is tl2)
print('EX4-3 - ', tl1 == tl3)
print('EX4-4 - ', tl1 is tl3)
EX4-1 - True
EX4-2 - True
EX4-3 - True
EX4-4 - False
4-3κ³Ό 4-4μ κ°μ κ²½μ°λ κ°μ κ°μ§λ§, κ°μ²΄λ λ€λ₯΄λ€λ κ²μ μ μ μλ€.
tl1.append(1000)
tl1[1].remove(105)
tl1μ 1000μ μΆκ°νκ³ , 1λ²μ§Έ 리μ€νΈμμ 105λ₯Ό μ κ±°νλ©΄ μ΄λ»κ² λ κΉ !
print('EX4-5 - ', tl1)
print('EX4-6 - ', tl2)
print('EX4-7 - ', tl3)
EX4-5 - [10, [100], (5, 10, 15), 1000]
EX4-6 - [10, [100], (5, 10, 15), 1000]
EX4-7 - [10, [100], (5, 10, 15)]
tl2λ κ°μ κ°μ²΄λ‘ 볡μ¬λμ΄ μκΈ° λλ¬Έμ tl1κ³Ό κ°μ κ²μ λ³Ό μ μλ€. κ·Έλ°λ° tl3λ λ°λ§ λκ°λ€! μ κ·Έλ΄κΉ?
print(id(tl1[1])) # tl1κ³Ό tl3μ idλ λ€λ₯΄μ§λ§ μμ μλ 리μ€νΈμ idκ°μ κ°λ€. !
print(id(tl3[1]))
2324415643528
2324415643528
μ . tl1κ³Ό tl3 μ체 κ°μ²΄μ idκ°μ λ¬λμ§λ§, κ·Έ μμ μλ 1λ²μ§Έ 리μ€νΈμ idκ°μ κ°μλ€. μ μ μ΄ λ°©λ²μΌλ‘ νλ 볡μ¬μ νμ μ΄ λ³΄μ΄κΈ° μμνλ€.
tl1[1] += [110, 120]
tl1[2] += (110, 120)
tl1μ 리μ€νΈμ ννμ κ°κ° κ°μ μΆκ°ν΄μ€λ€.
print('EX4-8 - ', tl1)
print('EX4-9 - ', tl2) # νν μ¬ ν λΉ (깩체 μλ‘ μμ±) ---> λ°μ΄ν°κ° νμ ν λ§μ λ μ£Όμ ν΄μΌ νλ€. κ°μ²΄λ₯Ό μλ‘ μμ±νλ€λ κ²μ λ°μ΄ν° μμ€μ΄ μμ μλ μκ³ , λ©λͺ¨λ¦¬λ₯Ό λ μ°¨μ§ν μλ μκ² λκΈ° λλ¬Έ !
print('EX4-10 - ', tl3)
EX4-8 - [10, [100, 110, 120], (5, 10, 15, 110, 120), 1000]
EX4-9 - [10, [100, 110, 120], (5, 10, 15, 110, 120), 1000]
EX4-10 - [10, [100, 110, 120], (5, 10, 15)]
μ°μ tl1, tl2λ μ¬κΈ°μλ λκ°μ΄ μμ§μΈ λ€λ κ²μ μ μ μλ€. κ·Έλ°λ° tl3μμλ μ‘°κΈ μ΄μν νμμ΄ μΌμ΄λ¬λ€. 리μ€νΈλ κ°μ΄ μΆκ°λμμ§λ§ , ννμ κ°μ΄ μΆκ°λμ§ μμλ€. λ΅μ μ½λ μ£Όμμ μ¨μλ―μ΄, ννμ κ°μ²΄λ₯Ό μλ‘ μμ±νμ¬ μ¬ ν λΉμ΄ λκΈ° λλ¬ΈμΈλ°, μ΄ν΄λ₯Ό μνμ¬ idκ°μ μΆλ ₯ν΄λ³΄μλ€.
# 리μ€νΈ, ννμ κ° μΆκ° μ
print(id(tl1[2]))
print(id(tl2[2]))
print(id(tl3[2]))
...
# 리μ€νΈ, ννμ κ° μΆκ° ν
print(id(tl1[2]))
print(id(tl2[2]))
print(id(tl3[2]))
1367137568664
1367137568664
1367137568664
1367137218056
1367137218056
1367137568664
리μ€νΈμ²λΌ tl1, tl3μ νν idκ°μ κ°μλ€. ννμμ μ΄κ² μ΄μκ° λλ κ²μ΄λ€! tl1[2] += (110, 120) <--- μ΄ μ½λκ° μ€νλλ©΄ ννμ΄ μ¬ν λΉλμ΄ μ κ°μ²΄κ° μμ±λλ€. κ·Έλμ idκ°μ 보면 μ΄ μ½λ μ κ³Ό νμ tl1, tl2 idκ°μ΄ λ¬λΌμ§ κ²μ λ³Ό μ μλ€. κ·Έλμ μ κ³Ό κ°μ idκ°μ κ°μ§ tl3μ ννμ λ³ννμ§ μλ κ²μ΄λ€.
*** copy & Deep copy (κΉμ 볡μ¬) μ€μ΅ν΄λ³΄κΈ°
(ex) μ₯λ°κ΅¬λ ν΄λμ€
class Basket:
def __init__(self, products=None):
if products is None:
self._products = []
else:
self._products = list(products) # μλ‘μ΄ κ°μ²΄ μμ±
def put_prod(self, prod_name):
self._products.append(prod_name)
def del_prod(self, prod_name):
self._products.remove(prod_name)
_productsμ 물건μ λ΄κ³ , put_prod λ©μλλ‘ λ¬Όκ±΄ μΆκ° , del_prod λ©μλλ‘ λ¬Όκ±΄ μμ λ₯Ό νλ ν΄λμ€ μμ±!
import copy
basket1 = Basket(['Apple', 'Bag', 'TV', 'Snack', 'Water'])
basket2 = copy.copy(basket1) # μμ 볡μ¬
basket3 = copy.deepcopy(basket1) # κΉμ λ³΅μ¬ ---> κ°μ²΄ μμ μλ μΈμ€ν΄μ€ λ³μκΉμ§ μ°Έμ‘°
copyλ₯Ό importνμ¬ μμ 볡μ¬(copy.copy) μ κΉμ 볡μ¬(copy.deepcopy) λ₯Ό ν΄λ³Έλ€.
print('EX5-1 - ', id(basket1), id(basket2), id(basket3))
print('EX5-2 - ', id(basket1._products), id(basket2._products), id(basket3._products))
EX5-1 - 1367137770520 1367137770464 1367137771304
EX5-2 - 1367137799240 1367137799240 1367137626888
copyλ₯Ό μ¬μ©νλ©΄ κ°μ²΄ μ체λ λͺ¨λ λ€λ₯΄κ² λμ€λ κ²μ λ³Ό μ μλ€. EX5-2μμ μμ 볡μ¬μ κΉμ볡μ¬μ μ°¨μ΄μ μ΄ λ³΄μΈλ€. deepcopyλ κ°μ²΄ μμ μλ μΈμ€ν΄μ€ λ³μκΉμ§ μ°Έμ‘°λ₯Ό νκΈ° λλ¬Έμ λ³μμ κ°μ²΄κΉμ§ idμ κ°μ΄ λ€λ₯΄λ€!
basket1.put_prod('Orange')
basket2.del_prod('Snack')
basket1μ Orangeλ₯Ό λ£κ³ , Snackμ μμ νλ©΄ μ΄λ»κ² λ³νλμ§ νμΈν΄ 보μ!
# λΉ
λ°μ΄ν°λ, μΉκ°λ°ν λ μ£Όμν΄μΌ νλ€.
print('EX5-3 - ', basket1._products)
print('EX5-4 - ', basket2._products)
print('EX5-5 - ', basket3._products)
EX5-3 - ['Apple', 'Bag', 'TV', 'Water', 'Orange']
EX5-4 - ['Apple', 'Bag', 'TV', 'Water', 'Orange']
EX5-5 - ['Apple', 'Bag', 'TV', 'Snack', 'Water']
μ΄λ κ² basket1κ³Ό basket2λ μλ‘ μν₯μ λ°λλ€. κΉμ볡μ¬λ₯Ό ν basket3λ μν₯μ λ°μ§ μλλ€.
μ΄λ λΉ λ°μ΄ν° λλ μΉκ°λ°μ ν λ μ£Όμν΄μΌ νλ€. μλ₯Ό λ€μ΄ λΉ λ°μ΄ν° λΆμμ ν λ μλ³Έμ μμμν€μ§ μκ³ λ³΅μ¬λ₯Ό νμ¬ μ μ²λ¦¬λ₯Ό ν κ²½μ° μμ 볡μ¬λ₯Ό νκ² λλ©΄ μκ°λ§ ν΄λ μμ°νλ€.
- ν¨μ 맀κ°λ³μ μ λ¬ μ£Όμ
def mul(x, y):
x += y
return x
xμ yλ₯Ό λν΄μ xμ λ£κ³ , xλ₯Ό returnν΄μ£Όλ mul ν¨μ μμ±
x = 10
y = 5
print('EX6-1 - ', mul(x, y), x, y)
EX6-1 - 15 10 5
ν¨μ μμ xκ° 15λΌκ³ ν΄μ μ μλ³μ xμ κ°μ΄ λ³κ²½λμ§λ μλλ€.
* κ°λ³ν a -> μλ³Έ λ°μ΄ν° λ³κ²½/ ν¨μμ μ λ¬ν λ μλ³Έμ μ£ΌμκΉμ§ λκΉ
a = [10, 100]
b = [5, 10]
print('Ex6-2 - ', mul(a, b), a, b) # ---> νμ₯! ! ! ! !
Ex6-2 - [10, 100, 5, 10] [10, 100, 5, 10] [5, 10]
νμ§λ§ κ°λ³νμΈ λ¦¬μ€νΈ νμ μ aλ₯Ό mulμ λ£μΌλ©΄ μ μλ³μμΈ aμ κ°κΉμ§ λ³ν΄λ²λ¦°λ€. μ¦ μλ³Έλ°μ΄ν°κ° λ³κ²½λλ κ²μ΄λ€. ν¨μμ μ΄ λ°μ΄ν°λ₯Ό μ λ¬ν λ μλ³Έμ μ£ΌμκΉμ§ λκΈ°κΈ° λλ¬Έμ΄λ€.
* λΆλ³ν c ---> μλ³Έ λ°μ΄ν° λ³κ²½ μλ¨
c = (10, 100)
d = (5, 10)
print('Ex6-2 - ', mul(c, d), c, d)
λΆλ³νμΈ νννμ μ cκ°μ κ²½μ°μλ μλ³Έ λ°μ΄ν°μ λ³κ²½μ΄ λμ§ μλλ€. μμμ λ°°μ λ―μ΄ ννμ λ΄μ©μ΄ κ°±μ λ λλ μλ‘μ΄ μ£Όμμ ν λΉμ΄ λκΈ° λλ¬Έμ΄λ€.
- νμ΄μ¬ λΆλ³ν μμΈ
: str, bytes, frozenset, Tuple ---> μ¬λ³Έ μμ± X ---> μ°Έμ‘° λ°ν (κ°μ κ°μ΄λ©΄ id λ κ°λ€)
tt1 = (1, 2, 3, 4, 5)
tt2 = tuple(tt1)
tt3 = tt1[:]
μ΄λ²μ ννμ κ°κ° λ€λ₯Έ λ°©λ²μΌλ‘ 볡μ¬νμ¬ tt2μ tt3λ₯Ό μμ±νμλ€.
print('EX7-1 - ', tt1 is tt2, id(tt1), id(tt2))
print('EX7-2 - ', tt1 is tt3, id(tt1), id(tt3))
EX7-1 - True 1367134857000 1367134857000
EX7-2 - True 1367134857000 1367134857000
리μ€νΈλ μμ κ°μ λ°©λ²μΌλ‘ κ°μ²΄λ₯Ό μμ±νλ©΄ μλ‘μ΄ μ£Όμκ° λΆμ¬λμλ€. ννμ λΆλ³νμ΄λ―λ‘ κ°μ κ°μ΄λ©΄ idλ κ°λ€. λ°λΌμ λͺ¨λ idκ°μ΄ κ°κ² λμλ€. ννμ λΆλ³νμ΄κΈ° λλ¬Έμ ννμ ννμ λν λ κΈ°μ‘΄ ννμ κ°μ΄ μΆκ°λλ κ°λ μ΄κΈ° 보λ€λ μλ‘μ΄ νν κ°μ²΄κ° λ§λ€μ΄μ§λ κ²μ΄λΌκ³ 보면 λλ€.
tt4 = (10, 20, 30, 40, 50)
tt5 = (10, 20, 30, 40, 50)
ss1 = 'Apple'
ss2 = 'Apple'
ννκ³Ό λλ€λ₯Έ λΆλ³ν strλ‘ κ²μ¦μ ν΄λ³΄κ³ λ§μΉλ€.
print('EX7-3 - ', tt4 is tt5, tt4 == tt5, id(tt4), id(tt5))
print('EX7-4 - ', ss1 is ss2, ss1 == ss2, id(ss1), id(ss2))
EX7-3 - True True 1367137216648 1367137216648
EX7-4 - True True 1367137769320 1367137769320
κ°μ΄ κ°μΌλ©΄ λ€λ₯Έ λ³μμ λ°λ‘ μμ±μ ν΄λ κ°μ μ£Όμλ₯Ό κ°κ³ μλ€λ κ²μ μ μ μλ€.