1
오류와 처리 다루기
(a)
1
2
3
for i in range(5):
x = 0
y = 3/x
—-> 3 y = 3/x ZeroDivisionError: division by zero
(b)
1
2
3
4
5
6
for i in range(5):
try:
x = 0
y = 3/x
except Exception:
print ("something wrong")
something wrong something wrong something wrong something wrong something wrong
2
inheritance 관련 예제
(a)
1
2
3
4
5
6
7
8
9
10
11
12
13
import math
class Vector2:
def __init__(self,x,y):
self._x = x
self._y = y
def x(self):
return self._x
def y(self):
return self._y
def size(self):
return math.sqrt(self.x()**2 + self.y()**2)
v1 = Vector2(1,1)
print(v1.size())
1.4142135623730951
(b)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import math
class Vector2:
def __init__(self,x,y):
self._x = x
self._y = y
def x(self):
return self._x
def y(self):
return self._y
def __sub__(self,other):
return Vector2(self.x() - other.x(), self.y() - other.y())
def __repr__(self):
return "Vector2({},{})".format(self.x(),self.y())
def size(self):
return math.sqrt(self.x()**2 + self.y()**2)
v1 = Vector2(1,0)
v2 = Vector2(0,1)
v = v1 - v2
print(v)
print(v.size())
Vector2(1,-1) 1.4142135623730951
(c)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import math
class Vector2:
def __init__(self,x,y):
self._x = x
self._y = y
def x(self):
return self._x
def y(self):
return self._y
def __sub__(self,other):
return Vector2(self.x() - other.x(), self.y() - other.y())
def __repr__(self):
return "Vector2({},{})".format(self.x(),self.y())
def size(self):
return math.sqrt(self.x()**2 + self.y()**2)
class Point2(Vector2):
def __init__(self,x,y):
super().__init__(x,y)
def distance(self,other):
v = self - other
return v.size()
p1 = Point2(1,0)
p2 = Point2(0,1)
print(p1.distance(p2))
1.4142135623730951
3
임의의 차원을 갖는 Vector와 그를 상속하는 Point
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import math
class Vector:
def __init__(self,lst,dim=None):
self._vec = list(lst)
if dim is None:
self._dim = len(self._vec)
else:
self._dim = dim
def dim(self):
return self._dim
def __repr__(self):
return "Vector({0})".format(self._vec)
def __str__(self):
if (self.dim() > 3):
return "Vector({},{},...)".format(self[0],self[1])
else:
return self.__repr__()
def __getitem__(self,key):
return self._vec[key]
def __setitem__(self,key,val):
self._vec[key] = val
def __add__(self,other):
return Vector([self[i] + other[i] for i in range(self.dim())])
def __sub__(self,other):
return Vector([self[i] - other[i] for i in range(self.dim())])
def size(self):
return math.sqrt(sum([x*x for x in self._vec]))
(a)
1
2
3
4
5
6
7
x = Vector(range(100))
y = Vector(range(100))
x[0] = 10
print('x-y=',x-y)
print('x+y=',x+y)
print('x[10]=',x[10])
print('(x-y).size()=',(x-y).size())
x-y= Vector(10,0,…) x+y= Vector(10,2,…) x[10]= 10 (x-y).size()= 10.0
(b)
1
2
3
4
5
6
7
8
9
10
11
12
13
class Point(Vector):
def __init__(self,lst):
super().__init__(lst)
def distance(self,other):
#d = super().__sub__(other)
#d = self.__sub__(other)
d = self - other
return d.size()
p1 = Point(range(100))
p2 = Point(range(100))
p1[0] = 10
print("p1.distance(p2)=",p1.distance(p2))
p1.distance(p2)= 10.0