6. def length(vector):
return math.sqrt(vector.x ** 2 + vector.y **2)
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
>>> v = Vector(3, 4)
>>> v.length = length
>>> v.length(v)
5.0
7. class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def length(self):
return math.sqrt(self.x ** 2 + self.y **2)
>>> v = Vector(3, 4)
>>> v.length()
5.0
8. global
self ?
class Vector:
xx = 3
yy = 4
def __init__(self, x, y):
xx = x
yy = y
def length(self):
return math.sqrt(xx ** 2 + yy **2)
>>> v = Vector(5, 12)
>>> v.xx
3
>>> v.yy
4
>>> v.length()
NameError: global name ‘xx’ is not defined
xx 3
yy 4
length
__init__
xx 3
yy 4
self.x
self.y
length
__init__
9. self? this?
class Vector:
def __init__(this, x, y):
this.x = x
this.y = y
def length(this):
return math.sqrt(this.x ** 2 + this.y **2)
>>> v = Vector(3, 4)
>>> v.length()
5.0
self는 파이썬 객체. 즉, 키워드가 아
니다.