blob: da5b3891d18c62f72824954c8be6ef987ced1015 [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)
30 """
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000031 def __init__(self, *v):
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000032 self.v = list(v)
Guido van Rossume8769491992-08-13 12:14:11 +000033
Georg Brandl5ada7c72010-07-05 20:13:41 +000034 @classmethod
35 def fromlist(cls, v):
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000036 if not isinstance(v, list):
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000037 raise TypeError
Georg Brandl5ada7c72010-07-05 20:13:41 +000038 inst = cls()
39 inst.v = v
40 return inst
Guido van Rossume8769491992-08-13 12:14:11 +000041
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000042 def __repr__(self):
Alexander Belopolsky31c27402010-07-05 21:44:05 +000043 args = ', '.join(repr(x) for x in self.v)
44 return 'Vec({})'.format(args)
Guido van Rossume8769491992-08-13 12:14:11 +000045
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000046 def __len__(self):
47 return len(self.v)
Guido van Rossume8769491992-08-13 12:14:11 +000048
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000049 def __getitem__(self, i):
50 return self.v[i]
Guido van Rossume8769491992-08-13 12:14:11 +000051
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000052 def __add__(self, other):
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000053 # Element-wise addition
Alexander Belopolsky31c27402010-07-05 21:44:05 +000054 v = [x + y for x, y in zip(self.v, other.v)]
Georg Brandl5ada7c72010-07-05 20:13:41 +000055 return Vec.fromlist(v)
Guido van Rossume8769491992-08-13 12:14:11 +000056
Raymond Hettingerfe63faa2003-09-10 21:12:59 +000057 def __sub__(self, other):
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000058 # Element-wise subtraction
Alexander Belopolsky31c27402010-07-05 21:44:05 +000059 v = [x - y for x, y in zip(self.v, other.v)]
Georg Brandl5ada7c72010-07-05 20:13:41 +000060 return Vec.fromlist(v)
Guido van Rossume8769491992-08-13 12:14:11 +000061
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000062 def __mul__(self, scalar):
63 # Multiply by scalar
Alexander Belopolsky31c27402010-07-05 21:44:05 +000064 v = [x * scalar for x in self.v]
Georg Brandl5ada7c72010-07-05 20:13:41 +000065 return Vec.fromlist(v)
Guido van Rossume8769491992-08-13 12:14:11 +000066
Alexander Belopolsky31c27402010-07-05 21:44:05 +000067 __rmul__ = __mul__
Guido van Rossume8769491992-08-13 12:14:11 +000068
69
70def test():
Alexander Belopolsky31c27402010-07-05 21:44:05 +000071 import doctest
72 doctest.testmod()
Guido van Rossume8769491992-08-13 12:14:11 +000073
74test()