简体   繁体   English

在此 python 代码中出现属性错误

[英]getting attribute error in this python code

Hello every one, when I run this code, I get a attribute error.大家好,当我运行这段代码时,我得到一个属性错误。 I used two diffrent ways to find the area of pyramid but the second way is not working.我使用了两种不同的方法来找到金字塔的区域,但第二种方法不起作用。 I do not know how to solve this problem.我不知道如何解决这个问题。 If you know how to solve this problem, please help me.如果你知道如何解决这个问题,请帮助我。 thanks a lot!多谢!

    class Rectangle:
        def __init__(self, length, width) -> None:
            self.length = length
            self.width = width

        def area(self) -> float:
            return self.length * self.width
    
        def perimeter(self) -> float:
            return 2 * self.length + 2 * self.width

        class Square(Rectangle):
            def __init__(self, length) -> None:
                super(Square, self).__init__(length, length)

        class Cube(Square):
            def surface_area(self) -> float:
                face_area = super(Square, self).area()
                return face_area * 6

            def volume(self) -> float:
                face_area = super(Square, self).area()
                return face_area * self.length

        class Triangle:
            def __init__(self, base, height) -> None:
                self.base = base
                self.height = height

            def tri_area(self) -> float:
                return 0.5 * self.base * self.height

        class RightPyramid(Square, Triangle):
            def __init__(self, base, slant_height) -> None:
                self.base = base
                self.slant_height = slant_height
                super().__init__(self.base)

            def area(self) -> float:
                base_area = super(Square, self).area()
                perimeter = super(Square, self).perimeter()
                return 0.5 * perimeter * self.slant_height + base_area

            def area_2(self) -> float:
                base_area = super(Square, self).area
                triangle_area = super(Triangle, self).tri_area()
                return triangle_area * 4 + base_area

        print(RightPyramid.__mro__) 
        pyramid = RightPyramid(3, 6)
        print(pyramid.area())
        print(pyramid.area_2())

I notice you are using inheritance all the way through your classes.我注意到您在整个课程中一直在使用 inheritance。

While I agree a Square is a subclass of Rectangle, a Cube in not a subclass of Squares, it is composed of Squares (hint, read up about composition vs inheritance).虽然我同意 Square 是 Rectangle 的子类,而不是 Squares 的子类中的 Cube,但它由 Squares 组成(提示,请阅读有关组合与继承的信息)。

Also since python 3, super() does not need arguments in most cases.此外,由于 python 3, super()在大多数情况下不需要 arguments。

There are a couple of problems in your code:您的代码中有几个问题:

  • RightPyramid.__init__ does one super().__init__(self.base) , so how should super(Triangle, self).tri_area() access the height ? RightPyramid.__init__做了一个super().__init__(self.base) ,那么super(Triangle, self).tri_area()应该如何访问height
  • In area_2 , you're accessing super(Square, self).area , not actually calling it.area_2中,您正在访问super(Square, self).area ,而不是实际调用它。

This is what I can offer you for your RightPyramid problem.这就是我可以为您提供的RightPyramid问题。

class Rectangle:
    def __init__(self, length, width) -> None:
        self.length = length
        self.width = width

    def area(self) -> float:
        return self.length * self.width

    def perimeter(self) -> float:
        return 2 * self.length + 2 * self.width


class Square(Rectangle):
    def __init__(self, length) -> None:
        super(Square, self).__init__(length, length)


class Triangle:
    def __init__(self, base, height) -> None:
        self.base = base
        self.height = height

    def tri_area(self) -> float:
        return 0.5 * self.base * self.height


class RightPyramid:
    def __init__(self, base, slant_height) -> None:
        self.base = Square(base)
        self.side = Triangle(base, slant_height)

    def area(self) -> float:
        base_area = self.base.area()
        perimeter = self.base.perimeter()
        return 0.5 * perimeter * self.side.height + base_area

    def area_2(self) -> float:
        base_area = self.base.area()
        triangle_area = self.side.tri_area()
        return triangle_area * 4 + base_area

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 python 代码中出现属性错误 - Getting an attribute error in python code #Python 为什么我不断收到此代码的命名元组属性错误? - #Python Why do I keep getting namedtuple attribute error for this code? Python入门:属性错误 - Getting Started with Python: Attribute Error 使用Python和Cursor获取属性错误 - Getting Attribute error with Python and Cursor 我在 python 中运行下面的代码并收到错误 'AttributeError: 'QgridWidget' object has no attribute 'to_excel'' - I am running the code below in python and getting the error 'AttributeError: 'QgridWidget' object has no attribute 'to_excel'' 尝试使用 Python 和 OpenCV 加载图像时,我在以下代码中收到“属性错误”? - I am getting 'Attribute Error' in the following code while tried loading an image, using Python & OpenCV? 对于 python 中的以下代码,我收到错误--AttributeError: 'str' object has no attribute 'next' - For the following code in python, I am getting the error--AttributeError: 'str' object has no attribute 'next' 在Python中使用函数时出现属性错误 - Getting an attribute error when working with function in Python 在python3中获取Nonetype属性错误 - Getting Nonetype Attribute error in python3 在Python中不断获取此“属性错误” - Getting this “Attribute error” over and over in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM