Vector Class - emulating numeric type
<p>I have a python class</p>

<pre><code>class Vector2D(object):

    def __init__(self, x, y):
        self.x = float(x)
        self.y = float(y)

    def mag(self):
        return sqrt(self.x**2 + self.y**2)

    ...
</code></pre>

<p>I want to be able to multiply vectors together like <code>vector1 * vector2</code>, so I added</p>

<pre><code>    def __mul__(self, v):
        return Vector2D(self.x * v.x, self.y * v.y)
</code></pre>

<p>But I also want to use <code>new_vector = some_vector * 2</code> and return  a new vector like so</p>

<pre><code>    def __mul__(self, factor):
        return Vector2D(self.x * factor, self.y * factor)
</code></pre>

<p>How do I do both?</p>
