I have this function in Python:
def Rotate_Vector(vector, axis, direction):
where vector is a tuple of 3 elements (each element represent the three coordinates x,y,z of the vector on Cartesian axes), axis are the coordinates of an axis, and direction is an integer representing the clockwise or counter-clockwise movement.
I want to control, in my funcion, if the input parameters are correctly:
- vector must be a tuple of 3 integer
- axis must be a tuple of 3 integer, and the possibile value should be -1, 0, 1
- direction must be an integer with value +1 or -1.
I would like to know the right way to do these controls (types, values and numbers of elements) in a function.
Edit:
axis could be 6 possibile cases: (1,0,0) (-1,0,0) (0,1,0) (0,-1,0) (0,0,1) (0,0,-1)
As said previously, python is duck typed, thus you simply do things as you normally would, and expect the user to deal with any exceptions raised.
It’s better just to unit-test your code, making sure everything works with valid numbers, which increases your confidence in certain things working, and allows you to more easily narrow down possible error locations.
If you really want to check the type, you could use an assertion on the type of the objects (e.g.
isinstance(direction, int)) for debugging purposes, but this is really just the “poor man’s unit test”.Using python’s principles (ask for forgiveness, not permission, and explicit is better than implicit), I would do something like this:
Since you really only care about the sign of direction, you can just use that and eliminate any error checking for it. The special case of
0will be treated as1, which you may want to raise aValueErrorfor.If you don’t actually need ax/ay/az or x/y/z, it may be better just to directly perform operations on
vectorandaxis, and let the underlying operations raise the exceptions. This will enable it for duck typing.Edit: (updated
axes->axisfor the new values in the question)