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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:29:20+00:00 2026-06-04T20:29:20+00:00

It feels like such a simple thing but I can’t seem to find the

  • 0

It feels like such a simple thing but I can’t seem to find the info I need.
Say I define a class Matrix:

class Matrix():
    def __mul__(self, other):
    if isinstance(other, Matrix):
        #Matrix multiplication.
    if isinstance(other, int): #or float, or whatever
        #Matrix multiplied cell by cell.

This work fine if I multiply a matrix by an int, but since int doesn’t know how to deal with matrices, 3*Matrix raises a TypeError.
How do I deal with this?

  • 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-04T20:29:21+00:00Added an answer on June 4, 2026 at 8:29 pm

    Define __rmul__ to override the calling of int()‘s __mul__ method:

    class Matrix():
        # code
    
        def __rmul__(self, other):
            #define right multiplication here.
    
            #As Ignacio said, this is the ideal
            #place to define matrix-matrix multiplication as __rmul__() will
            #override __mul__().
    
        # code
    

    Note that you can do this with all of the numeric operators.

    Also note that it’s better to use new style classes, so define your class as:

    class Matrix(object):
    

    This will allow you to do things like:

    if type(other) == Matrix: ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems like such a simple thing, but I can't find any obvious solutions...
It feels like there must be some semi-simple solution to this, but I just
This feels like a really basic question, but I can't figure it out anyway..
This feels like it should be pretty simple, but not much seems to be
This feels like a stupid question, but is there a simple control for WPF
I feel like a moron posting such simple questions on here, but the knowledge
I feel like such a noob asking this but, for some reason a horizontal
This feels like the kind of code that only fails in-situ, but I will
OK, this feels like an idiot question, but I'm stuck - I don't know
I need a generic collection class which I can add to, and enumerate over.

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.