What is the bug in this Python program?

-1

What is the bug in the following source code?
I am unable to find it myself.

ShapeBase.py

from abc import ABC

class ShapeBase(ABC):
    def __init__(self, idd: str):
        self.id_: str = idd
        self.cx_: float = 0.0
        self.cy_: float = 0.0

    @property
    def cx_(self) -> float:
        return self.cx_

    @cx_.setter
    def cx_(self, cx: float):
        self.cx_ = cx

    @property
    def cy_(self) -> float:
        return self.cy_

    @cy_.setter
    def cy_(self, cy: float):
        self.cy_ = cy

    def id(self) -> str:
        return self.id_

    def area(self) -> float:
        pass

Square.py

from shapes.ShapeBase import ShapeBase

class Square(ShapeBase):
    def __init__(self, idd: str, a: float):
        super().__init__(idd)
        self.a_ = a

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

    def width(self) -> float:
        return self.a_

main.py

from shapes.Square import Square

if __name__ == '__main__':
    s1 = Square('S1', 4.0)
    print("Area = " + str(s1.area()))
    

Output

C:\Users\pc\AppData\Local\Microsoft\WindowsApps\python3.7.exe C:/Users/pc/source/repos/Shapes/main.py

Process finished with exit code -1073741571 (0xC00000FD)

By the way, this problem is not about the name of the attributes.

This problem is related to inheritance.

python
debugging
pycharm
asked on Stack Overflow May 4, 2021 by user366312 • edited May 4, 2021 by tripleee

3 Answers

1

Your cx_ property is calling cx_.setter when you call self.cx_ = cx inside the same setter function. To fix this you need to rename the property inside __init__ or remove the property declarations because your property does the same thing as normal python defaults would have done.

Here is the code with internal properties renamed

from abc import ABC

class ShapeBase(ABC):
    def __init__(self, idd: str):
        self.id_: str = idd
        self._cx_: float = 0.0
        self._cy_: float = 0.0

    @property
    def cx_(self) -> float:
        return self._cx_

    @cx_.setter
    def cx_(self, cx: float):
        self._cx_ = cx

    @property
    def cy_(self) -> float:
        return self._cy_

    @cy_.setter
    def cy_(self, cy: float):
        self._cy_ = cy

    def id(self) -> str:
        return self.id_

    def area(self) -> float:
        pass
answered on Stack Overflow May 4, 2021 by xcodz-dot
1

https://pythonguide.readthedocs.io/en/latest/python/property.html#managing-attributes

Read the important box:

"If we use ‘return self.radius’ instead of ‘return self._radius’, then the @property will result in infinite loop as self.property will call the property method, which will return self.property, which results in calling the @property again."

Here the loop:

@property
def cx_(self) -> float:
    return self.cx_

@cx_.setter
def cx_(self, cx: float):
    self.cx_ = cx
answered on Stack Overflow May 4, 2021 by Yuri • edited May 4, 2021 by Yuri
0

I can see inside "ShapeBase" class you didn't inherited constructor of ABC class, although you've inherited ABC class from ShapeBase class.

You should inherit the base class constructor like the following code block.

super().__init__()

or,

ABC.__init__(self)

Also need to pass the ABC constructor arguments.

answered on Stack Overflow May 4, 2021 by Md. Mehedi Hasan

User contributions licensed under CC BY-SA 3.0