Cryptic error message: t.copy is not a function

I have run into some problems with the Vector class.

When you give is wrong arguments. Like when I called Vector.multiply(3, retreat). I got “t.copy is not a function” error.

I also seem to have had trouble just working with positions, but cannot recreate that problem at the moment, so ill get back if I Can.

What happens inside Vector.multiply()?

# This is not exactly the code, just a simplified version for explanation-purposes
def Vector.multiply(vec, num):
    copy = vec.copy()
    copy.multiply(num)
    return copy

As you see the code calls vec.copy() without checking if vec is actually a vector. Now what happens if we substitute the arguments with your values:

copy = 3.copy()
copy.multiply(retreat)
return copy

You see the problem? 3 is a number, you can not call a method from it. That is why order of arguments is extremely important. You cannot simply switch those around. Computers can be extremely smart yet incredibly stupid. Modern computers will happily steer a rocket to space for you… as long as you as programmer don’t switch arguments around.


Luckily we have smart programs that can check that before we actually launch the rocket. We only have to hope that the programmers of these programs didn’t make any error…

2 Likes

Same error here, although the code seems legit to me:

ahead = {"x": 5, "y": 0}
behind = Vector.multiply(ahead, -1)

Any ideas?


update: defining the variable explicitly as a vector fixes the issue:

ahead = Vector(5, 0)        # instead of {"x": 5, "y": 0}
behind = Vector.multiply(ahead, -1)

The strange thing is that e.g. Vector.add() works with both methods… Any explanations for that, @nick?

The first argument to one of these Vector methods should be a Vector. The second one (if it takes two) can be an object.

1 Like

Thanks for the answer.