Python_2 - jjin-choi/study_note GitHub Wiki
Β§ ν΄λμ€μ κ°μ²΄
1. μΈμ€ν΄μ€μ λ¬Έμμ΄ ννμ
- μΈμ€ν΄μ€μ λ¬Έμμ΄ ννμμ λ°κΎΈλ €λ©΄ str() μ repr() λ©μλλ₯Ό μ μνλ€.
- κ°μ²΄λ₯Ό μΆλ ₯ν κ²½μ° κΈ°λ³Έμ μΌλ‘λ str() ν¨μκ° νΈμΆλλ€.
- !r μ κΈ°λ³Έκ°μΌλ‘ repr() ν¨μκ° νΈμΆλλ€.
- { } κΈ°λ³Έκ°μΌλ‘ str() ν¨μκ° νΈμΆλλ€.
- repr() ν¨μλ κ°μ²΄λ₯Ό μ μΈνμ λ νν κ·Έλλ‘λ₯Ό λ§λ€μ΄μΌ ν¨.
- νν κ·Έλλ‘ λΌλ건, μλ₯Ό λ€μ΄ μλμμ Pair(A, B) λΌκ³ νλ©΄ repr λ κ·Έλλ‘ μΆλ ₯λλλ‘ ν΄μΌνλ€.
- λμ€μ eval(repr(x)) == x μ κ°μ textλ₯Ό λ§λλ κ²μ΄ νμ€μ΄λ€.
- repr(x) λ₯Ό eval μ λ£μΌλ©΄ μ€μ κ°μ²΄κ° μμ±λλ€.
1 class Pair:
2 def __init__(self, x, y):
3 self.x = x
4 self.y = y
5
6 def __repr__(self):
7 return f'Pair ({self.x!r}, {self.y!r})'
8
9 def __str__(self):
10 return f'({self.x}, {self.y})'
11
12 p = Pair(3, 4)
13 print (p)
2. κ°μ²΄μ μ½ν μ€νΈ κ΄λ¦¬ νλ‘ν μ½ (with) μ§μ
- κ°μ²΄κ° context management protocol (with) μ μ§μνκ² κ΅¬ν
- μμΌλ‘ λ§λλ κ³Όμ μ μλν ν λ νΈλ¦¬νλ€.
- with λ¬Έ λ§λλ©΄ enter() λ©μλκ° νΈμΆ
- with λ¬Έ λΉ μ Έ λκ°λλ exit() λ©μλκ° νΈμΆ
- κ°μ²΄μ λ΄λΆ ν¨μλ‘ κ΅¬νν΄λμ !
c = LazeConnection(('www.python.org', 80))
with c as s: # β c μ __enter__() νΈμΆ
...
# β c μ __exit__() νΈμΆ
print ("End")
- user κ° νμν λλ§ socketμ λ§λ€κΈ° μν΄ LazyConnection classλ₯Ό λ§λ€μ΄λ³΄μ.
- with c as s : λ₯Ό νλ©΄ enter() μ return κ°μ΄ as s λ‘ return λλ€.
- with λ΄λΆμ μ½λ (context) κ° μ μ μ’ λ£ λμκ±°λ λΉμ μ μ’ λ£ λμλλΌλ exit() μ΄ λ°λμ μ€νλμ΄μΌ νλ κ²½μ°μλ with λ‘ νλ©΄ λλ€.
3. μΈμ€ν΄μ€λ₯Ό λ§μ΄ μμ±ν λ λ©λͺ¨λ¦¬ μ μ½
-
κ°μ²΄ μΈμ€ν΄μ€λ§λ€ λ΄λΆμ λ©€λ²λ₯Ό μ μ₯ν λͺ©μ μΌλ‘ λμ λ리 (μνλ²³ μμΌλ‘ ꡬμ±) λ₯Ό ꡬμ±νλ€.
- μ΄λ λ©λͺ¨λ¦¬ λλΉμ μμΈμ΄ λλ€.
- λ΄λΆ ꡬ쑰λ dict μ΄ root λ§ κ°λ¦¬ν€κ³ μμ
- λλ¨Έμ§λ μ€μκ° μ λ ¬ λ° λΉ λ₯Έ κ²μμ μν RB treeλ‘ λμ΄μλ€. λ©λͺ¨λ¦¬ λλΉκ° μ¬νλ€.
- RB tree μ΄κΈ° λλ¬Έμ κ°μ²΄μ memberκ° μλλΌλ λμ μΌλ‘ μμ± / μ κ±°κ° κ°λ₯νλ€.
. μ¦, d1.name = "kim" μ΄λ° μμΌλ‘ d1 μ΄λΌλ κ°μ²΄μ memberμ name μ΄ μμ΄λ μ€κ°μ μΆκ° κ°λ₯νλ€.
. λν del d1.name μ μ¬μ©νμ¬ member λ₯Ό μμ ν μλ μλ€. - νμ§λ§ λͺ¨λ κ°μ²΄μμ μ΄λ° κΈ°λ₯μ κΌ νμλ‘ νμ§ μλλ€..
- μ΄λ° κ²½μ°μλ RB tree κΈ°λ₯μ μμ κ³ compact ν λ°°μ΄λ‘ κ΄λ¦¬νλ λ°©λ²μ΄ μλλ°, λ°λ‘ slots μ μ°λ©΄ λλ€.
-
__slots__μ μ μνλ©΄ νμ΄μ¬μ μΈμ€ν΄μ€μ ν¨μ¬ λ μμΆλ λ΄λΆ ννμμ μ¬μ©νλ€.
- μΈμ€ν΄μ€ λ§λ€ λμ λ리λ₯Ό ꡬμ±νμ§ μκ³ ννμ΄λ 리μ€νΈ κ°μ λΆνΌκ° μμ κ³ μ λ°°μ΄λ‘ μΈμ€ν΄μ€κ° λ§λ€μ΄μ§λ€.
- μ΄λ κ² νλ©΄ dict μ΄ μ¬λΌμ§κ² λλ€.
- λν, λ©€λ² μμ± / μ κ±°κ° λΆκ°λ₯νλ€.
1 class Date:
2 __slots__ = ['year', 'month', 'day']
3
10 print (dir(d1))
- special member : dict
- d1.dir (νΉμ dir(d1)) μ΄λΌκ³ μ€ννλ©΄ λ΄λΆ member μλ dict κ° μ‘΄μ¬νλ€.
4. κ΄λ¦¬ μμ± λ§λ€κΈ° #except #exception #setter #TypeError
- μΈμ€ν΄μ€ μμ±μ μ»κ±°λ μ€μ ν λ μΆκ°μ μΈ μ²λ¦¬ (type check, κ²μ¦ λ±)μ νκ³ μΆμλ @property μ΄μ©νλ€.
- ν¨μ λ΄λΆμμ μ¬μ©λλ λ³μμλ _λ₯Ό λΆμ¬μ μ¬μ©νλ€.
- python μμλ μ κ·Ό μ§μ μ (private, public) μ΄ μκΈ° λλ¬Έμ getter setter μ μ¬μ©ν νμκ° μμ.
- μ¬μ€μ κ°μ²΄ member μ κ°μ λ³κ²½νλκ² λ¬Έμ κ° μμ§λ§, μμΈ μ²λ¦¬λ₯Ό λ£κΈ° μν΄ getter, setter λ₯Ό λ£μ΄μ€ μλ μ½λλ‘λ μΆ©λΆν κ°λ₯.
- κ·Έλ°λ° get_X, set_X ν¨μλ₯Ό λ§λ€κ³ set_X μ μμΈ μ²λ¦¬λ₯Ό λ£μ΄μ£Όλ κ²λ λλλ° μ decorator μ μΈκΉ?
class Person:
def __init__(self, first_name):
self.first_name = first_name
def get_first_name(self):
return self.first_name
def set_first_name(self, value):
if not isinstance(value, str):
raise TypeError('Expected a string')
self.first_name = value
if __name__ == '__main__':
a = Person('Guido')
print (a.get_first_name())
a.set_first_name('Dave')
print (a.get_first_name())
try:
a.set_first_name(42)
except TypeError as e:
print (e)
(20.08.04 06:20 λΆν° λ€μ 보기)
- @λ decorator μ΄κ³ property ν¨μμ getter, setterλ₯Ό λ겨주면 λλ€.
- property λΌλ κ°μ²΄κ° μ‘΄μ¬νκ³ , μ¬κΈ°μ first_name μ λ£μ΄μ λ€μ first_name μΌλ‘ λ°μμ¨ κ²μ first_name.setterμ μ§μ΄λ£μ κ±Έ λ€μ first_name μΌλ‘ λ£μ΄μ€
- property κ°μ²΄ μ΄λ¦μλ€κ° setterλ₯Ό μ§μ΄ λ£μ΄μ£Όλ©΄ λλ€.
- @first_name.setter λ‘ λ°μ½λ μ΄ν°λ₯Ό μ§μ νλ©΄ ν΄λΉ μμ±μ λ³κ²½νλ € ν λ μλμΌλ‘ νΈμΆλλ€.
- μ΄ λ νμ μ κ²μ¬ν μ μκ³ λ¬Έμμ΄μ΄ μλ κ²½μ° μμΈλ₯Ό λμ§λ€.
1 class Person:
2 def __init__(self, first_name):
3 self.first_name = first_name
4
5 @property
6 def first_name(self):
7 return self._first_name
8
9 '''
10 def first_name(self):
11 return self._first_name
12
13 first_name = property(get_first_name)
14 '''
15
16 @first_name.setter
17 def first_name(self, value):
18 if not isinstance(value, str):
19 raise TypeError('Expected a string')
20 self._first_name = value
21
22 '''
23 first_name = first_name.setter(set_first_name)
24 '''
25
27 if __name__ == '__main__':
28 a = Person('Guido')
29 print(a.first_name)
30
31 a.first_name = 'Dave'
32 print(a.first_name)
33
34 try:
35 a.first_name = 42
36 except TypeError as e:
37 print (e)
5. λΆλͺ¨ ν΄λμ€μ λ©μλ νΈμΆ
- λΆλͺ¨μ λ©μλλ₯Ό νΈμΆνλ €λ©΄ super() ν¨μλ₯Ό μ¬μ©νλ€.
1 class A:
2 def spam(self):
3 print ('A.spam')
4
5 class B(A):
6 def spam(self):
7 print ('B.spam')
8 super().spam()
9
10
11 if __name__ == '__main__':
12 b = B()
13 b.spam()
- super()λ μΌλ°μ μΌλ‘ init() λ©μλμμ λΆλͺ¨λ₯Ό μ λλ‘ μ΄κΈ°ν νκΈ° μν΄ μ¬μ©λλ€.
16 class A:
17 def __init__(self):
18 print ('\nA.__init__() : ', end='')
19 self.x = 0
20
21 class B(A):
22 def __init__(self):
23 super().__init__()
24 print ('\nB.__init__() : ', end='')
25 self.y = 1
26
27 if __name__ == '__main__':
28 b = B()
29 print (b.x, b.y)
- proxy λ리μ λμμΈ pattern ...
-
python μ νΉλ³ λ©μλλ₯Ό μ€λ²λΌμ΄λν μ½λμμ super()λ₯Ό μ¬μ©νκΈ°λ νλ€.
-
getattr(self, name): μμ μκ² μλ μμ±μ μ κ·Ό νμ λ νΈμΆλ¨. κ·Έ λ κ·Έ μμ± (λ³μ) μ΄λ¦μ΄ name μΌλ‘ μ λ¬λλ€.
-
μμ μκ² μλ μμ±μ μ κ·Όνμ λμλ __getattr__μλ μ κ·Όνμ§ μμ. μκΈ°κ° ν¬ν¨νκ³ μλ λ©€λ²μΈ _obj μ μ κ·Όν λμλ νΈμΆλμ§ μμ.
-
μλ μ½λμμ pλ aμ λκ°μ μν μ νκ³ μκ² λλ€.
-
get μΌλ‘ κΊΌλ΄μ€λ 건 Proxy κ° ν μ μλλ° κ³Όμ° set λ κ°λ₯ν κΉ ?
-
setattr(self, name, value): μμ μκ² μκ±°λ μλ κ²½μ° νΈμΆλ¨. μ¦ λ¬΄μ‘°κ±΄ νΈμΆλ¨ !
-
setattr() ꡬνμ μ΄λ¦ νμΈμ΄ λ€μ΄ μλ€. λ§μ½ μ΄λ¦μ΄ λ°μ€λ‘ μμνλ©΄ super()λ₯Ό μ¬μ©ν΄μ setattr() μ μλ ꡬνμ νΈμΆνλ€.
-
1 class Proxy:
2 def __init__(self, obj):
3 self._obj = obj
4
5 def __getattr__(self, name):
6 print (f"Proxy.__getattr__(name = {name})")
7 return getattr(self._obj, name)
8
9 def __setattr__(self, name, value):
10 print (f"Proxy.__setattr__(name = {name}, value = {value})")
11 if name.startswith('_'):
12 # λ΄ ν¨μλ μ΄λ―Έ μ€λ²λΌμ΄λ© λμκΈ° λλ¬Έμ λΆλͺ¨μͺ½μ μλ __setattr__ μ μ¬μ©ν΄μ£Όλ©΄ λλ€.
13 super().__setattr__(name, value)
14 else:
15 setattr(self._obj, name, value)
16
17 if __name__ == '__main__':
18 class A:
19 def __init__(self, x):
20 self.x = x
21 def spam(self):
22 print ("A.spam")
25 a = A(42)
26 p = Proxy(a)
27
28 print (p.x)
29 print (p._obj)
30 print (p.spam())
31
32 p.x = 37
33 print ('should be 37 : ', p.x)
34 print ('should be 37 : ', a.x)
# output
''' 맨 μ²μμ __init__ ν¨μμμ _obj λ₯Ό set ν λμλ __setattr__ μ΄ νΈμΆλλ€.
Proxy.__setattr__(name = _obj, value = <__main__.A object at 0x7f863c9e4668>)
Proxy.__getattr__(name = x)
42
<__main__.A object at 0x7f863c9e4668>
Proxy.__getattr__(name = spam)
A.spam
None
Proxy.__setattr__(name = x, value = 37)
Proxy.__getattr__(name = x)
should be 37 : 37
should be 37 : 37
- λ€μ΄μλͺ¬λ μμ
- μλ μ½λμ²λΌ C μμ λΆλͺ¨λ₯Ό κ°κ° νΈμΆνλ©΄ base μ μ΄κΈ°νκ° 2λ² μ΄κΈ°ν λμ΄λ²λ¦°λ€.
1 class Base:
2 def __init__(self):
3 print ('Base.__init__')
4
5 class A(Base):
6 def __init__(self):
7 Base.__init__(self)
8 print ('A.__init__')
10 class B(Base):
11 def __init__(self):
12 Base.__init__(self)
13 print ('B.__init__')
14
15 class C(A,B):
16 def __init__(self):
17 A.__init__(self)
18 B.__init__(self)
19 print ('C.__init__')
20
21 if __name__ == '__main__':
22 c = C()
- C.mro λΌλ λ©€λ²κ° μλ€. (method resolution order)
- superκ° λΆλͺ¨μͺ½μ νΈμΆμ ν λ μ΄λ€ κ²½λ‘λ‘ νΈμΆν μ§μ λν order
- python μμλ baseλ₯Ό νλ²λ§ νΈμΆν΄μ£ΌκΈ° μν λ°©λ²μ΄ νμνλ°, λ°λ‘ super()λ₯Ό μ¬μ©νλ©΄ λλ€.
1 class Base:
2 def __init__(self):
3 print ('Base.__init__')
4
5 class A(Base):
6 def __init__(self):
7 #Base.__init__(self)
8 super().__init__()
9 print ('A.__init__')
10
11 class B(Base):
12 def __init__(self):
13 #Base.__init__(self)
14 super().__init__()
15 print ('B.__init__')
16
17 class C(A,B):
18 def __init__(self):
19 #A.__init__(self)
20 #B.__init__(self)
21 super().__init__()
22 print ('C.__init__')
23
24
25 if __name__ == '__main__':
26 c = C()
27 print (C.__mro__)
- C μμ λΆλͺ¨κ° A μμ λΆλͺ¨κ° B μμ λΆλͺ¨κ° Base μΈ κ².
- μ A μμ λΆλͺ¨κ° B μ΄μ§ ?
- C κ° μμλ μμλλ‘ super λ₯Ό κ²°μ νλ€. (class C(A, B) : λΌκ³ νκΈ° λλ¬Έμ Cμ λΆλͺ¨λ A, B)
- ν¨μλ₯Ό μ°Ύλ μμκ° C.mro
# output
(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class '__main__.Base'>, <class 'object'>)
6. μλΈν΄λμ€μμ νλ‘νΌν° νμ₯
-
deleter
- property κ° del λ‘ μ§μμ§λ €κ³ ν λ νΈμΆλλ ν¨μ
-
μλΈ ν΄λμ€μμ, λΆλͺ¨ ν΄λμ€μ μ μν νλ‘νΌν°μ κΈ°λ₯μ νμ₯ νκ³ μΆμ λ
- super() κ΄νΈ μμλ μ¬μ€ self λ‘ νΈμΆλλ€.
- getter μμλ μλ΅ν΄λ λλλ° setter μ deleter μμλ μλ΅νλ©΄ μλ¬κ° λ°μνλ€.
- super(SubPerson, XX) XX μ리μλ μμ ν΄λμ€μ νμ μ΄λ μΈμ€ν΄μ€ νμ μ λ£μ μ μλ€.
- XX μ리μ λ£λ κ°μ, λΆλͺ¨μ μλ class λ³μμΈμ§, instance λ³μμΈμ§μ λ°λΌ λ€λ₯΄λ€.
- class λ³μμ μ κ·Όνλ €κ³ ν λμλ class typeμ λ°λμ μ§μ ν΄μΌ νλ€. (???)
- λΆλͺ¨ λ©μλμ λλ¬νκΈ° μν μ μΌν λ°©λ²μ, μΈμ€ν΄μ€ λ³μκ° μλ ν΄λμ€ λ³μλ‘ μ κ·Όν΄μΌ νλ€.
- super(SubPerson, SubPerson) μ΄λ° μμΌλ‘ ...
-
property λ₯Ό 보면,
- name = property(name) <- property μ κ°μ²΄μΌ λΏ, self.name μ΄ μλλ€.
- μ¦ name μ class λ³μμ΄λ€.
1 class Person:
2 def __init__(self, name):
3 self.name = name
4
5 # getter
6 @property
7 def name(self):
8 return self._name
9
10 # setter
11 @name.setter
12 def name(self, value):
13 if not isinstance(value, str):
14 raise TypeError("Expected a string")
15 self._name = value
16
17 # deleter
18 @name.deleter
19 def name(self):
20 raise AttributeError("Can't delete attribute")
7. μλ‘μ΄ ν΄λμ€λ μΈμ€ν΄μ€ μμ± λ§λ€κΈ°
-
descriptor
- get set delete λ₯Ό μ¬μ μν class λ₯Ό descriptor λΌκ³ νλ€.
- μμ ν μλ‘μ΄ μ’ λ₯μ μΈμ€ν΄μ€ μμ±μ λ§λ€λ €λ©΄, κ·Έ κΈ°λ₯μ λμ€ν¬λ¦½ν° ν΄λμ€ ννλ‘ μ μν΄μΌ νλ€.
- property λ dscriptor ꡬν체 μ€ νλμ΄λ€.
class Integer:
def __init__(self, name):
self.name = name
def __get__(self, instance, cls):
if instance is None:
return self
else:
return instance.__dict__[self.name]
def __set__(self, instance, value):
if not isinstance(value, int):
raise TypeError('Expected an int')
instance.__dict__[self.name] = value
def __delete__(self, instance):
del instance.__dict__[self.name]
class Point:
x = Integer('x')
y = Integer('y')
def __init__(self, x, y):
self.x = x
self.y = y
if __name__ == '__main__':
p = Point(2, 3)
print(p.x)
p.y = 5
try:
p.x = 2.3
except TypeError as e:
print(e)
8. μλ£ κ΅¬μ‘° μ΄κΈ°ν λ¨μννκΈ°
- μλ£ κ΅¬μ‘°λ‘ μ¬μ©νλ ν΄λμ€λ₯Ό μμ±νκ³ μλλ°, λ°λ³΅μ μΌλ‘ λΉμ·ν init() ν¨μλ₯Ό λ§€λ² μμ±ν΄μΌ νλ€.
- Struct λΌλ λΆλͺ¨ ν΄λμ€μμ init μ νλ² ν΄μ£Όλ©΄ μμ class μμ μ€λ²λΌμ΄λ© νμ§ μλ ν, λΆλͺ¨μ init μ΄ νΈμΆλλ€.
class Structure:
_fields= []
def __init__(self, *args):
# argument μκ° κ° classμ field μμ κ°μμ§ check
if len(args) != len(self._fields):
raise TypeError('Expected {} arguments'.format(len(self._fields)))
for name, value in zip(self._fields, args):
setattr(self, name, value)
# κ° class λ§λ€ μμ μ field λ₯Ό μ μνκ³ κ·Έμ λ§κ² init λλλ‘ νλ€.
if __name__ == '__main__':
class Stock(Structure):
_fields = ['name', 'shares', 'price']
class Point(Structure):
_fields = ['x','y']
class Circle(Structure):
_fields = ['radius']
def area(self):
return math.pi * self.radius ** 2
if __name__ == '__main__':
s = Stock('ACME', 50, 91.1)
print(s.name, s.shares, s.price)
p = Point(2,3)
print(p.x, p.y)
c = Circle(4.5)
print(c.radius)
try:
s2 = Stock('ACME', 50)
except TypeError as e:
print(e)
- κ°λ³μΈμ λΏλ§ μλλΌ ν€μλ κ°λ³μΈμλ λ°κΈ° μν΄μλ μλμ κ°μ΄ ꡬννλ€.
- args κ° field λ³΄λ€ μκΈ°λ§ νλ©΄ λ¨
class Structure:
_fields= []
def __init__(self, *args, **kwargs):
if len(args) > len(self._fields):
raise TypeError('Expected {} arguments'.format(len(self._fields)))
for name, value in zip(self._fields, args):
setattr(self, name, value)
# args λ€μλΆν°λ ν€μλ μΈμλ₯Ό λ°μμ£Όλ©΄ λλ€.
for name in self._fields[len(args):]:
setattr(self, name, kwargs.pop(name))
if kwargs:
raise TypeError('Invalid argument(s): {}'.format(','.join(kwargs)))
if __name__ == '__main__':
class Stock(Structure):
_fields = ['name', 'shares', 'price']
s1 = Stock('ACME', 50, 91.1)
s2 = Stock('ACME', 50, price=91.1)
s3 = Stock('ACME', shares=50, price=91.1)
- λλ extra_args μ΄μ©ν΄μ μλμ κ°μ΄ ꡬνλ κ°λ₯
_fields= []
def __init__(self, *args, **kwargs):
if len(args) != len(self._fields):
raise TypeError('Expected {} arguments'.format(len(self._fields)))
for name, value in zip(self._fields, args):
setattr(self, name, value)
print (f"[TEST] kwargs.keys() - self._fields")
extra_args = kwargs.keys() - self._fields
for name in extra_args:
setattr(self, name, kwargs.pop(name))
if kwargs:
raise TypeError('Duplicate values for {}'.format(','.join(kwargs)))
if __name__ == '__main__':
class Stock(Structure):
_fields = ['name', 'shares', 'price']
s1 = Stock('ACME', 50, 91.1)
s2 = Stock('ACME', 50, 91.1, date='6/1/2020')
10. μΈν°νμ΄μ€, μΆμ λ² μ΄μ€ ν΄λμ€ μ μ
- body κ° μλ μΆμ ν¨μλ₯Ό λ§λ€μ΄ 보μ
- module : ABCMeta, abstractmethod (μΆμ ν΄λμ€, μΆμ λ©μλ) β» ABC : ABstraCt
- κΈ°λ³Έμ μΌλ‘ μΆμ ν¨μκ° νλλΌλ μλ ν΄λμ€λ κ°μ²΄κ° λ μ μμ
- abstractmethod λ₯Ό λ°μ½λ μ΄ν°λ ν΄λΉ ν¨μλ₯Ό μΆμ λ©μλλ‘ λ§λ€μ΄μ€λ€.
- a = IStream() μ νλ©΄ μλ¬ λ°μ
- μΆμ λ©μλ μ΄κΈ° λλ¬Έμ λ°λμ read write λ₯Ό ꡬνν΄μΌ ν¨.
- IStream ν΄λμ€λ μμν ν΄λμ€μκ² specμ ꡬμ±λμ΄μλ λͺ¨λ ν¨μ (μ¬κΈ°μλ, readν¨μμ writeν¨μ)λ₯Ό ꡬννλλ‘ κ°μ νλ€.
from abc import ABCMeta, abstractmethod
class IStream(metaclass=ABCMeta):
@abstractmethod
def read(self, maxbytes=-1):
pass
@abstractmethod
def write(self, data):
pass
class SocketStream(IStream):
def read(self, maxbytes=-1):
print('reading')
def write(self, data):
print('writing')
def serialize(obj, stream):
if not isinstance(stream, IStream):
raise TypeError('Expected an IStream')
print('serializing')
if __name__ == '__main__':
try:
a = IStream()
except TypeError as e:
print(e)
a = SocketStream()
a.read()
a.write('data')
serialize(None, a)
import sys
try:
serialize(None, sys.stdout)
except TypeError as e:
print(e)
import io
IStream.register(io.IOBase)
serialize(None, sys.stdout)
from abc import ABCMeta, abstractmethod
class A(metaclass=ABCMeta):
# κ°μ ν¨μλ‘ λ¨Όμ λ§λ€κ³ property λ λ€λ₯Έ decorator λ₯Ό μ¨μ€λ€.
@property
@abstractmethod
def name(self):
pass
@name.setter
@abstractmethod
def name(self, value):
pass
@classmethod
@abstractmethod
def method1(cls):
pass
@staticmethod
@abstractmethod
def method2():
pass
10 μ 04:10 λΆν° λ€μ 보기
11 λΆν° λ€μ 보기
13. μν μλ£ κ΅¬μ‘°μμ λ©λͺ¨λ¦¬ κ΄λ¦¬
-
μνμ΄ μλ μλ£ κ΅¬μ‘°λ₯Ό μμ±νλ νλ‘κ·Έλ¨μμ λ©λͺ¨λ¦¬ κ΄λ¦¬μ λ¬Έμ κ° μμ
-
μνΈ μ°Έμ΄κ° μλ ꡬ쑰μμλ weakref λΌμ΄λΈλ¬λ¦¬λ₯Ό μ¬μ©νλ μ½ν μ°Έμ‘°λ‘ λ§λλ κ²μ κ³ λ €νλ€.
-
ref count μ, root = Node ('parent') λ₯Ό νλ μκ°, μκΈ° μμ μ΄ κ°λ¦¬ν€κΈ° λλ¬Έμ, ref = 1 μ΄ λκ³ , μμκ³Ό μνΈ μ°Έμ΄ λλ μκ°, (root.add_child(c1)) ref = 2 κ° λλ€.
-
λ°λΌμ del root λ§ νκ² λλ©΄ root κ° μκΈ° μμ μ κ°λ¦¬ν€μ§ μκ² λμ ref = 1 μ΄ λ λΏ, Node μ체λ λ¨μμμ΄ memory leak μ΄ λ°μνλ€.
class Data:
def __del__(self):
print('Data.__del__')
class Node:
def __init__(self):
self.data = Data()
self.parent = None
self.children = []
def add_child(self, child):
self.children.append(child)
child.parent = self
if __name__ = "__main__":
a = Data()
del a
a = Node()
a.add_child(Node())
- μ΄λ₯Ό μν΄μ gc (garbage collector) λ₯Ό μ§μΈ μ μλ€.
import gc
gc.collect()
- νμ§λ§ κ³μ μ΄λ₯Ό μνν΄ μ€ μ μκΈ° λλ¬Έμ, weak pointer λ₯Ό μ¬μ©νμ !