i have add 2 names using overloading operator add. output should both names added example: "john" + "frank" = 'johnfrank'
class person(): def __init__(self, name): self.aname = name def __add__(self,other): x = self.person1 y = self.person2 print(x + y) return(couple(x,y)) class couple(person): def __init__(self,person1,person2): self.person1 = person1 self.person2 = person2 what doing wrong?
the error get:
traceback (most recent call last): file "<pyshell#81>", line 1, in <module> acouple = p1 + p2 file "/users/desktop/namep.py", line 7, in __add__ x = self.person1 attributeerror: 'person' object has no attribute 'person1'
a person object has aname should pass currect object self , added object:
def __add__(self, other): return couple(self, other)
Comments
Post a Comment