Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

use python language please. Research the __radd__ method. Explain in a paragraph

ID: 3594018 • Letter: U

Question

use python language please.

Research the __radd__ method. Explain in a paragraph how it differs from __add__.

When is it used? Implement __radd__ in the Fraction class

class Fraction:
def __init__(self, top, bottom):
self.num = top
self.den = bottom

def __add__(self, otherFraction):
newNum = self.num * otherFraction.den +
self.den * otherFraction.num
newDen = self.den * otherFraction.den
common = self.gcd(newNum, newDen)
return Fraction(newNum // common, newDen // common)

def __eq__(self, other):
firstNum = self.num * other.den
secondNum = other.num * self.den
return firstNum == secondNum
  
def gcd(self, m, n):
while m % n != 0:
oldm = m
oldn = n
m = oldn
n = oldm % oldn
return n
  
def __str__(self):
return str(self.num) + "/" + str(self.den)

Explanation / Answer

__radd__ is called if left operand does not support the add operation and operands are of different types while __add__ is called when both operand are of same type

class Fraction:
def __init__(self, top, bottom):
self.num = top
self.den = bottom
def __add__(self, otherFraction):
newNum = self.num * otherFraction.den +
self.den * otherFraction.num
newDen = self.den * otherFraction.den
common = self.gcd(newNum, newDen)
return Fraction(newNum // common, newDen // common)
def __eq__(self, other):
firstNum = self.num * other.den
secondNum = other.num * self.den
return firstNum == secondNum
  
def gcd(self, m, n):
while m % n != 0:
oldm = m
oldn = n
m = oldn
n = oldm % oldn
return n
  
def __str__(self):
return str(self.num) + "/" + str(self.den)
  
def __radd__(self, other):
if isinstance(other, Fraction):
newNum = self.num * other.den + self.den * other.num
newDen = self.den * other.den
common = self.gcd(newNum, newDen)
return Fraction(newNum // common, newDen // common)
return NotImplemented

# copy pastable code link: https://paste.ee/p/UA4GM