@@ -53,29 +53,30 @@ def __str__(self):
5353 def __repr__ (self ):
5454 return '<{:.6g}, {:.6g}, {:.6g}>' .format (self ._x , self ._y , self ._z )
5555
56- def __add__ (self ,other ):
57- return vector (self ._x + other ._x , self ._y + other ._y , self ._z + other ._z )
56+ def __add__ (self , other ):
57+ if type (other ) is vector :
58+ return vector (self ._x + other ._x , self ._y + other ._y , self ._z + other ._z )
59+ return NotImplemented
5860
59- def __sub__ (self ,other ):
60- return vector (self ._x - other ._x , self ._y - other ._y , self ._z - other ._z )
61+ def __sub__ (self , other ):
62+ if type (other ) is vector :
63+ return vector (self ._x - other ._x , self ._y - other ._y , self ._z - other ._z )
64+ return NotImplemented
6165
6266 def __truediv__ (self , other ): # used by Python 3, and by Python 2 in the presence of __future__ division
63- try :
67+ if isinstance ( other , ( float , int )) :
6468 return vector (self ._x / other , self ._y / other , self ._z / other )
65- except :
66- raise TypeError ('a vector can only be divided by a scalar' )
69+ return NotImplemented
6770
6871 def __mul__ (self , other ):
69- try :
72+ if isinstance ( other , ( float , int )) :
7073 return vector (self ._x * other , self ._y * other , self ._z * other )
71- except :
72- raise TypeError ('a vector can only be multiplied by a scalar' )
74+ return NotImplemented
7375
7476 def __rmul__ (self , other ):
75- try :
77+ if isinstance ( other , ( float , int )) :
7678 return vector (self ._x * other , self ._y * other , self ._z * other )
77- except :
78- raise TypeError ('a vector can only be multiplied by a scalar' )
79+ return NotImplemented
7980
8081 def __eq__ (self ,other ):
8182 if type (self ) is vector and type (other ) is vector :
0 commit comments