Python Forum

Full Version: Assertion Error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone,
I've got problem with assertion. It seems that it should work, but after running the program I receive AssertionError. Do you have any idea why and how I can fix it/modify declaration of classes? Think
This is my code:
class Figure1:
    def __init__(self, x, y, z):
        self._x = x
        self._y = y
        self._z = z
    @property
    def area(self):
        return (self._x + self._y) * self._z / 2
class Figure2(Figure1):
    def __init__(self, x, z):
        super().__init__(x=x, y=x, z=z)
class Figure3(Figure2):
    @property
    def circumference(self):
        return self._x * 4

class Figure4(Figure2):

    def __init__(self, x, y):
        super().__init__(x=x,z=y)
    @property
    def circumference(self):
        return 2 * self._x + 2 * self._z

class Figure5(Figure4, Figure3):

    def __init__(self, x):
        super().__init__(x=x, y=x)

a = Figure5(9).area
print(a)
b = Figure5(9).circumference
print(b)
if __name__ == "__main__":
    Figure5.x = 9
    assert Figure5.circumference == 36
    assert Figure5.area == 81


and the error:
Error:
81.0 Traceback (most recent call last): 36 line 37, in <module> assert Figure5.circumference == 36 AssertionError Process finished with exit code 1
You should be creating a Figure5 instance: check = Figure5(5). Run your assertions on that. Currently you are testing the class itself.
"Figure5" is a class, not an instance of a class. As such, there is no "self" to provide to circumference(). If you remove the property annotation from circumference, a second error appears because of the lack of self. You need to instantiate the class and then call circumference to make it work.

On a side note, you may encounter issues with Figure5 because of multiple inheritance because both of its parents have a circumference method.

class Figure1:
    def __init__(self, x, y, z):
        self._x = x
        self._y = y
        self._z = z

    def area(self):
        return (self._x + self._y) * self._z / 2

class Figure2(Figure1):
    def __init__(self, x, z):
        super().__init__(x=x, y=x, z=z)

class Figure3(Figure2):
    
    def circumference(self):
        return self._x * 4

class Figure4(Figure2):

    def __init__(self, x, y):
        super().__init__(x=x,z=y)

    def circumference(self):
        return 2 * self._x + 2 * self._z

class Figure5(Figure4, Figure3):

    def __init__(self, x):
        super().__init__(x=x, y=x)
 
a = Figure5(9)
print(a.area())
print(a.circumference())

if __name__ == "__main__":
    a.x = 9
    assert a.circumference() == 36
    assert a.area() == 81