@@ -53,29 +53,30 @@ def __str__(self):
53
53
def __repr__ (self ):
54
54
return '<{:.6g}, {:.6g}, {:.6g}>' .format (self ._x , self ._y , self ._z )
55
55
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
58
60
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
61
65
62
66
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 )) :
64
68
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
67
70
68
71
def __mul__ (self , other ):
69
- try :
72
+ if isinstance ( other , ( float , int )) :
70
73
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
73
75
74
76
def __rmul__ (self , other ):
75
- try :
77
+ if isinstance ( other , ( float , int )) :
76
78
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
79
80
80
81
def __eq__ (self ,other ):
81
82
if type (self ) is vector and type (other ) is vector :
0 commit comments