Python Forum
operator overloading won't work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
operator overloading won't work
#1
Hi,

When i run this code i get
(0) + (1) = (2)
while i should get
Rood Groen Blauw + Geel Paars Cyaan = Rood Groen Blauw Geel Paars Cyaan
What am i doing wrong?
Any input is much appreciated!


class MyClass:
    def __init__(self, *args):
        self.Input = args

    def __add__(self, Other):
        Output = MyClass()
        Output.Input = self.Input + Other.Input
        return Output

    def __str__(self):
        Output = ""
        for Item in Self.Input:
            Output += Item
            Output += " "
        return Output

Value1 = MyClass("Rood", "Groen", "Blauw")
Value2 = MyClass("Geel", "Paars", "Cyaan")
Value3 = Value1 + Value2

print("(0) + (1) = (2)"
        .format(Value1, Value2, Value3))
Reply


Messages In This Thread
operator overloading won't work - by MaartenRo - Aug-04-2020, 02:03 AM
RE: operator overloading won't work - by deanhystad - Aug-04-2020, 02:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Operator Overloading issue mp3909 4 2,287 Jun-07-2020, 06:34 PM
Last Post: buran
  unexpected sub result after overloading operator jolinchewjb 1 2,291 Jan-24-2019, 08:23 AM
Last Post: buran
  Program not running (Overloading problem) anurag123 2 2,586 Feb-19-2018, 07:23 PM
Last Post: nilamo
  Overloading error PyMan 1 2,330 Feb-19-2018, 04:57 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

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