title: how can I overwrite __mul__ function in python
body: <p>I wrote a class in python like this </p>
  
  <pre><code>class Vector(object):
  def __init__(self, coordinates):
      try:
          if not coordinates:
              raise ValueError
          self.coordinates = tuple(coordinates)
          self.dimension = len(coordinates)
  
      except ValueError:
          raise ValueError('The coordinates must be nonempty')
  
      except TypeError:
          raise TypeError('The coordinates must be an iterable')
  
  def __add__(self,v):
      v1 = np.array(self.coordinates)
      v2 = np.array(v.coordinates)
      result = v1 + v2
      return result.tolist()
  
  def __sub__(self, other):
      v1 = np.array(self.coordinates)
      v2 = np.array(other.coordinates)
      result = v1 - v2
      return result.tolist()
  
  def __mul__(self, other):
      return other * np.array(self.coordinates)
  
  def multiply(self,other):
      v = Decimal(str(other)) * np.array(self.coordinates)
      return v
  
  def __str__(self):
      return 'Vector: {}'.format(self.coordinates)
  
  
  def __eq__(self, v):
      return self.coordinates == v.coordinates
  </code></pre>
  
  <p>I want to overwrite the operation <code>*</code>, so I can achieve function like:</p>
  
  <pre><code>3*Vector([1,2,3])=Vector([3,6,9])
  </code></pre>
  
  <p>so I tried code like this:</p>
  
  <pre><code>    def __mul__(self, other):
      return other * np.array(self.coordinates)
  </code></pre>
  
  <p>however, I was disappointed to notice that this function only works when</p>
  
  <pre><code>Vector([1,2,3])*3
  </code></pre>
  
  <p>if I wrote:</p>
  
  <pre><code>3*Vector([1,2,3])
  </code></pre>
  
  <p>it says:</p>
  
  <blockquote>
    <p>TypeError: unsupported operand type(s) for *: 'int' and 'Vector'</p>
  </blockquote>
  
  <p>How can I get the function that works both on <code>3*Vector([1,2,3])</code> and <code>Vector([1,2,3])*3</code>?</p>
  
  <p>thank you so much.</p>