Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
95 views
in Technique[技术] by (71.8m points)

'int' object has no attribute 'den' error message in Python

def gcd(m, n):
    while m % n != 0:
        m, n = n, m % n
    return n


class Fraction:
    def __init__(self, top, bottom):
        if not isinstance(top, int):
            raise ValueError("Numenator should be an integer.")
        if not isinstance(bottom, int):
            raise ValueError("Denomenator should be an integer.")
        common = gcd(top, bottom)
        self.num = top // common
        self.den = bottom // common

    def __str__(self):
        return "{:d}/{:d}".format(self.num, self.den)

    def __eq__(self, other_fraction):
        first_num = self.num * other_fraction.den
        second_num = other_fraction.num * self.den
        return first_num == second_num

    
    
    
    def __add__(self, other_fraction):
        new_num = self.num * other_fraction.den 
                  + self.den * other_fraction.num
        new_den = self.den * other_fraction.den
        return Fraction(new_num, new_den)
    #__radd__ is only called if the left object does not have an __add__ method, or that method does not know how to add the two objects.
    def __radd__(self, other_fraction):
        new_num = self.num * other_fraction.den 
                  + self.den * other_fraction.num
        new_den = self.den * other_fraction.den
        return Fraction(new_num, new_den)
   

    def show(self):
        print("{:d}/{:d}".format(self.num, self.den))

    def get_num(self):
        return self.num

    def get_den(self):
        return self.den
    
    def __sub__(self, other_fraction):
        new_num = self.num * other_fraction.den 
                  - self.den * other_fraction.num
        new_den = self.den * other_fraction.den
        return Fraction(new_num, new_den)
    
    def __mul__ (self, other_fraction):
        new_num = self.num * other_fraction.num
        new_den = self.den * other_fraction.den
        return Fraction(new_num, new_den)
    
    def __truediv__(self, other_fraction):
        new_num = self.num * other_fraction.den
        new_den = self.den * other_fraction.num
        return Fraction(new_num, new_den)
    
    def __gt__(self,other_fraction):
        return self.num*other_fraction.den>self.den*other_fraction.num
    
    def __ge__(self,other_fraction):
        return self.num*other_fraction.den>=self.den*other_fraction.num
    
    def __lt__(self,other_fraction):
        return self.num*other_fraction.den<self.den*other_fraction.num
    
    def __le__(self,other_fraction):
        return self.num*other_fraction.den<=self.den*other_fraction.num
    
    def __ne__(self,other_fraction):
        return self.num!=other_fraction.num and self.den!=other_fraction.num

So this is what I am doing now and when I run this code with

print(x + 1)
print(1 + x)

these two, I get 'int' object has no attribute 'den' this error message.

What am I doing wrong?

question from:https://stackoverflow.com/questions/65948529/int-object-has-no-attribute-den-error-message-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

By accessing other_fraction.den, your __add__ method assumes the second operand of an add operation to have a den attribute, and yet by doing x + 1 you're passing to it 1, an int object, which does not have a den attribute, as the second operand.

You can work around this by making 1 a Fraction object first before performing an add operation:

x + Fraction(1, 1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...