-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo1.py
More file actions
37 lines (27 loc) · 747 Bytes
/
demo1.py
File metadata and controls
37 lines (27 loc) · 747 Bytes
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
28
29
30
31
32
33
34
35
36
37
# -*- coding: utf-8 -*-
import math
class Vector:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
# 字符串表示形式
def __repr__(self):
return 'Vector(%r, %r)' % (self.x, self.y)
# hypot求模
def __abs__(self):
return math.hypot(self.x, self.y)
def __bool__(self):
return bool(abs(self))
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
def __mul__(self, other):
return Vector(self.x*other, self.y*other)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
v2 = Vector(1, 2)
v3 = Vector(2, 3)
v1 = Vector(1, 2)
print(v1 == v2)
print(v1 == v3)
x = [i for i in range(10)]
print(x)