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 8175563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:49:46+00:00 2026-06-06T22:49:46+00:00

I have a vector class and I defined the __mul__ method to multiply a

  • 0

I have a vector class and I defined the __mul__ method to multiply a vector by a number.

Here is the __mul__ method :

def __mul__(self, other):
    x = self.x * other
    y = self.y * other
    new = Vector()
    new.set_pos((x, y))
    return new

My problem is that I don’t know which is which between the number and the vector.
If self is the number, self.x raises an error. (I’m maybe mistaking on this point : Is “other” always a number ?)

So I found here : Python: multiplication override
that I could do :

__rmul__ = __mul__

but how can I do that in a class definition ?

Something like :

def __rmul__ = __mul__
  • 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-06T22:49:47+00:00Added an answer on June 6, 2026 at 10:49 pm

    self will never be the number in __mul__() because the object the method is attached to is not the number, it’s the vector, and by definition it’s the multiplicand.

    other will be a number if your object is being multiplied by a number. Or it could be something else, such as another vector, which you could test for and handle.

    When your object is the multiplier, __rmul__() is called if the multiplicand doesn’t know how to handle the operation.

    To handle the case in which __mul__ and __rmul__ should be the same method, because the operation is commutative, you can just do the assignment in your class definition.

    class Vector(object):
        def __mul__(self, other):
            pass
    
        __rmul__ = __mul__
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector of pointers to class objects. These class objects invoke new
I have an in-place vector class that's defined like so: template<class T> class svectorbase
I have a 3D vector class. The private variables are defined: union { struct
I have defined a vector like this in the header file class entry {
If I have a const vector defined in my class, how can I go
I have a private attribute in a class that is defined as vector<pair<char *,
I have a std::vector of some class of the form class A{ public: A():i(someNumber){}
I have a vector of pointers to a class. I need to call their
I have a vector called players and a class called Player. And what I'm
I have a vector v of objects A in a Class B where each

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.