Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7762703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:28:35+00:00 2026-06-01T14:28:35+00:00

I have this function in Python: def Rotate_Vector(vector, axis, direction): where vector is a

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:28:36+00:00Added an answer on June 1, 2026 at 2:28 pm

    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:

    import math
    
    def rotate_vector(vector, axis, direction):
        try:
            x, y, z = vector
        except TypeError:
            raise TypeError("Invalid vector {0}".format(vector))
    
        valid_axes = {(1,0,0), (-1,0,0), (0,1,0), (0,-1,0), (0,0,1), (0,0,-1)}
    
        if not axis in valid_axes:
            raise ValueError("Invalid axis {0}".format(axis))
    
        try:
            ax, ay, az = axis
        except TypeError:
            raise TypeError("Invalid axis {0}".format(axis))
    
        # do math to rotate the vector
        # rotated = ...
    
        try:
            # You really only need the sign of the direction
            return math.copysign(rotated, direction)
            # or:
            return rotated * math.copysign(1, direction)
        except TypeError:
            raise TypeError("Invalid direction {0}".format(direction))
    

    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 0 will be treated as 1, which you may want to raise a ValueError for.

    If you don’t actually need ax/ay/az or x/y/z, it may be better just to directly perform operations on vector and axis, and let the underlying operations raise the exceptions. This will enable it for duck typing.

    Edit: (updated axes -> axis for the new values in the question)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic question on python functions and parameters Given this function: def
Suppose I have this function signature: def foo(a=True, b=True, c=True, d=True, e=True): I've decided
Does anyone know how to convert this function from Python to PHP? I have
I have the following function in a Python module called dictbuilder.py: def my_dictbuilder(reader_o, writer):
I have a very frustrating python problem. In this code fixedKeyStringInAVar = SomeKey def
I have one question about sys.setrecursionlimit() From the python docs this function: Set the
I know python functions are virtual by default. Let's say I have this: class
Not many are aware of this feature, but Python's functions (and methods) can have
I have this function in my Javascript Code that updates html fields with their
I have this function in my head: <head> window.onload = function(){ var x =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.