Python Forum
Magic Method Arithmetic Operators
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Magic Method Arithmetic Operators
#1
Is it possible to use more than 2 parameters for magic methods/Dunders for arithmetic operators? The following is my code

class Point():
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __str__(self):
        return f"Point ({self.x}, {self.y})"

    def __add__(self, other):
        return Point(self.x+other.x, self.y+other.y)

point = Point(1, 2)

other = Point(3, 5)
print(point+other)
I want to get something like
def __add__(self, other, something_else):
        return Point(self.x+other.x+something_else.x, self.y+other.y+something_else.y)
Also, for some reason the indentations dont show, so please ignore that :)
Larz60+ write Jan-10-2021, 10:43 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use code tags on future posts.
Reply
#2
ClownPrinceOfCrime Wrote:for some reason the indentations dont show, so please ignore that
We will certainly not ignore that. Instead, you will read the BBCode help page to learn how to make the indentation show.

About the issue, how would you invoke your extended __add__ operator? If you write
obj + other + something_else
in the code, it will call
obj.__add__(other).__add__(something_else)
and not
obj.__add__(other, something_else)
buran likes this post
Reply
#3
the familiar arithmetic operations of addition, subtraction, multiplication, etc. are binary operation
ndc85430 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
If say you had a load of points in a collection that you wanted to add up, that could neatly be done with reduce.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Use of if - and operators Pedro_Castillo 1 487 Oct-24-2023, 08:33 AM
Last Post: deanhystad
  Mixing Boolean and comparison operators Mark17 3 1,405 Jul-11-2022, 02:20 AM
Last Post: perfringo
  magic related field in Django model sonh 1 1,234 Apr-24-2022, 12:37 PM
Last Post: sonh
  Evaluating arithmetic expression with recursion. muddybucket 3 2,829 Dec-17-2021, 08:31 AM
Last Post: deanhystad
  Simple arithmetic question ebolisa 5 2,038 Dec-15-2021, 04:56 PM
Last Post: deanhystad
  Need a little help with numpy array magic. pmf71 0 1,141 Dec-01-2021, 02:51 AM
Last Post: pmf71
  Class and Operators in Python rsherry8 1 1,987 May-27-2020, 07:09 PM
Last Post: buran
  Trying Comparison Operators PythonGainz 3 2,702 Mar-28-2020, 10:46 AM
Last Post: PythonGainz
  Mathematical Operators in String AgileAVS 1 2,380 Mar-04-2020, 04:14 PM
Last Post: Gribouillis
  A doubt with 'in' and 'not in' operators with strings newbieAuggie2019 7 3,584 Oct-23-2019, 03:11 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020