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

  • Home
  • SEARCH
  • 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 7901733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:18:22+00:00 2026-06-03T09:18:22+00:00

I recently learned about operator overloading in python and I would like to know

  • 0

I recently learned about operator overloading in python and I would like to know if the following is possible.

Consider the following hypothetical/contrived class.

class My_Num(object):
    def __init__(self, val):
        self.val = val

    def __add__(self, other_num):
        if isinstance(other_num, My_Num):
            return self.val + other_num.val
        else:
            return self.val + other_num

I know that the way that’s written above, I can do things like this

n1 = My_Num(1)
n2 = My_Num(2)
n3 = 3
print n1 + n2
print n1 + n3

and those will work as expected. I also know that the way it’s currently written I can’t do this

n1 = My_Num(1)
n2 = 2
print n2 + n1

Is there any way around this? I know this example is contrived but I have an application in which it would be very useful if when I did operator overloading, the class for which I define the operator can appear on the right hand side of operator. Is this possible in python?

  • 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-03T09:18:24+00:00Added an answer on June 3, 2026 at 9:18 am

    Yes. For example, there is __radd__. Also, there are none for __le__(), __ge__(), etc., but as Joel Cornett rightly observes, if you define only __lt__, a > b calls the __lt__ function of b, which provides a workaround.

    >>> class My_Num(object):
    ...     def __init__(self, val):
    ...         self.val = val
    ...     def __radd__(self, other_num):
    ...         if isinstance(other_num, My_Num):
    ...             return self.val + other_num.val
    ...         else:
    ...             return self.val + other_num
    ... 
    >>> n1 = My_Num(1)
    >>> n2 = 3
    >>> 
    >>> print n2 + n1
    4
    >>> print n1 + n2
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for +: 'My_Num' and 'int'
    

    Note that in at least some cases it’s reasonable to do something like this:

    >>> class My_Num(object):
    ...     def __init__(self, val):
    ...         self.val = val
    ...     def __add__(self, other_num):
    ...         if isinstance(other_num, My_Num):
    ...             return self.val + other_num.val
    ...         else:
    ...             return self.val + other_num
    ...     __radd__ = __add__
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just recently learned about friend class concept in C++ (I've googled around for
I recently learned about normalisation in my informatics class and I'm developing a multiplayer
I recently learned JavaScript an all of the sudden I hear about Python... Should
I just recently learned about the Properties class in java, and I'm trying to
I've recently learned that python doesn't have the switch/case statement. I've been reading about
I learned about Lazy class in .Net recently and have been probably over-using it.
I recently learned about the , operator and the fact that it introduces a
I recently learned about Twitter Bootstrap CSS , and wonder what other pre-fabricated stylesheets
I recently learned about the basic structure of the .docx file (it's a specially
Recently I learned about Werkzeug's interactive debugger in combination of django_extensions to debug Django

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.