blob: 6df1f50a8998e160888c9d3636c4c22b8a8fb850 [file] [log] [blame]
Georg Brandl856898b2010-12-30 22:11:50 +00001#!/usr/bin/env python3
Guido van Rossume8769491992-08-13 12:14:11 +00002
Georg Brandl856898b2010-12-30 22:11:50 +00003"""
4A demonstration of classes and their special methods in Python.
5"""
6
7class Vec:
8 """A simple vector class.
9
10 Instances of the Vec class can be constructed from numbers
Alexander Belopolsky31c27402010-07-05 21:44:05 +000011
12 >>> a = Vec(1, 2, 3)
13 >>> b = Vec(3, 2, 1)
14
15 added
16 >>> a + b
17 Vec(4, 4, 4)
18
19 subtracted
20 >>> a - b
21 Vec(-2, 0, 2)
22
23 and multiplied by a scalar on the left
24 >>> 3.0 * a
25 Vec(3.0, 6.0, 9.0)
26
27 or on the right
28 >>> a * 3.0
29 Vec(3.0, 6.0, 9.0)
Raymond Hettingerd69ae752021-03-15 19:53:58 -070030
31 and dot product
32 >>> a.dot(b)
33 10
34
35 and printed in vector notation
36 >>> print(a)
37 <1 2 3>
38
Alexander Belopolsky31c27402010-07-05 21:44:05 +000039 """
Raymond Hettingerd69ae752021-03-15 19:53:58 -070040
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000041 def __init__(self, *v):
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000042 self.v = list(v)
Guido van Rossume8769491992-08-13 12:14:11 +000043
Georg Brandl5ada7c72010-07-05 20:13:41 +000044 @classmethod
45 def fromlist(cls, v):
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000046 if not isinstance(v, list):
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000047 raise TypeError
Georg Brandl5ada7c72010-07-05 20:13:41 +000048 inst = cls()
49 inst.v = v
50 return inst
Guido van Rossume8769491992-08-13 12:14:11 +000051
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000052 def __repr__(self):
Raymond Hettingerd69ae752021-03-15 19:53:58 -070053 args = ', '.join([repr(x) for x in self.v])
54 return f'{type(self).__name__}({args})'
55
56 def __str__(self):
57 components = ' '.join([str(x) for x in self.v])
58 return f'<{components}>'
Guido van Rossume8769491992-08-13 12:14:11 +000059
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000060 def __len__(self):
61 return len(self.v)
Guido van Rossume8769491992-08-13 12:14:11 +000062
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000063 def __getitem__(self, i):
64 return self.v[i]
Guido van Rossume8769491992-08-13 12:14:11 +000065
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000066 def __add__(self, other):
Raymond Hettingerd69ae752021-03-15 19:53:58 -070067 "Element-wise addition"
Alexander Belopolsky31c27402010-07-05 21:44:05 +000068 v = [x + y for x, y in zip(self.v, other.v)]
Georg Brandl5ada7c72010-07-05 20:13:41 +000069 return Vec.fromlist(v)
Guido van Rossume8769491992-08-13 12:14:11 +000070
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000071 def __sub__(self, other):
Raymond Hettingerd69ae752021-03-15 19:53:58 -070072 "Element-wise subtraction"
Alexander Belopolsky31c27402010-07-05 21:44:05 +000073 v = [x - y for x, y in zip(self.v, other.v)]
Georg Brandl5ada7c72010-07-05 20:13:41 +000074 return Vec.fromlist(v)
Guido van Rossume8769491992-08-13 12:14:11 +000075
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000076 def __mul__(self, scalar):
Raymond Hettingerd69ae752021-03-15 19:53:58 -070077 "Multiply by scalar"
Alexander Belopolsky31c27402010-07-05 21:44:05 +000078 v = [x * scalar for x in self.v]
Georg Brandl5ada7c72010-07-05 20:13:41 +000079 return Vec.fromlist(v)
Guido van Rossume8769491992-08-13 12:14:11 +000080
Alexander Belopolsky31c27402010-07-05 21:44:05 +000081 __rmul__ = __mul__
Guido van Rossume8769491992-08-13 12:14:11 +000082
Raymond Hettingerd69ae752021-03-15 19:53:58 -070083 def dot(self, other):
84 "Vector dot product"
85 if not isinstance(other, Vec):
86 raise TypeError
87 return sum(x_i * y_i for (x_i, y_i) in zip(self, other))
88
Guido van Rossume8769491992-08-13 12:14:11 +000089
90def test():
Alexander Belopolsky31c27402010-07-05 21:44:05 +000091 import doctest
92 doctest.testmod()
Guido van Rossume8769491992-08-13 12:14:11 +000093
94test()