Issue #4426: The UTF-7 decoder was too strict and didn't accept some legal sequences.
[python.git] / Demo / classes / Vec.py
blob56cb839392ae38bde636313857216207a85d3321
1 # A simple vector class
4 def vec(*v):
5 return Vec(*v)
8 class Vec:
10 def __init__(self, *v):
11 self.v = list(v)
13 def fromlist(self, v):
14 if not isinstance(v, list):
15 raise TypeError
16 self.v = v[:]
17 return self
19 def __repr__(self):
20 return 'vec(' + repr(self.v)[1:-1] + ')'
22 def __len__(self):
23 return len(self.v)
25 def __getitem__(self, i):
26 return self.v[i]
28 def __add__(self, other):
29 # Element-wise addition
30 v = map(lambda x, y: x+y, self, other)
31 return Vec().fromlist(v)
33 def __sub__(self, other):
34 # Element-wise subtraction
35 v = map(lambda x, y: x-y, self, other)
36 return Vec().fromlist(v)
38 def __mul__(self, scalar):
39 # Multiply by scalar
40 v = map(lambda x: x*scalar, self.v)
41 return Vec().fromlist(v)
45 def test():
46 a = vec(1, 2, 3)
47 b = vec(3, 2, 1)
48 print a
49 print b
50 print a+b
51 print a-b
52 print a*3.0
54 test()