Click here to Skip to main content
14,838,477 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
import math

class TwoDObject:
    def __init__(self,x=0,y=0):
        self.x = x
        self.y = y
    def area(self):
        return 0
    def move(self,xchange=0,ychange=0):
        self.x = self.x + xchange
        self.y = self.y + ychange

class Circle(TwoDObject):
    def __init__(self,radius:float=1,**kwargs):
        self.radius = radius
    def area(self):
    def size_change(self,percent:float):
        self.radius = self.radius * (percent/100)

class Sector(Circle):
    def __init__(self,angle:float=180,**kwargs):
        self.angle = angle
    def area(self):
        return super().area() * self.angle/360

class Rectangle(TwoDObject):
    def __init__(self,height:float=1,width:float=1,**kwargs):
        self.width = width
        self.height = height
    def area(self):
        return self.width * self.height
    def size_change(self,percent:float):
        self.width = self.width * (percent/100)
        self.height = self.height * (percent/100)

if __name__ == "__main__":
    c  =  Circle(5,11,22)
    r = Rectangle(100,100,20,20)

What I have tried:

the code throws an error that there are more arguments for Circle objject,but actually the circle class take 3 parameters but in fact exactly 3 arguments to the c objcect are given 

the type error :
Traceback (most recent call last):
  File "", line 46, in <module>
    c  =  Circle(5,11,22)
TypeError: __init__() takes from 1 to 2 positional arguments but 4 were given
Updated 7-Jul-20 21:31pm
Luc Pattyn 7-Jul-20 22:38pm

I don't speak Python, but my Google foo is OK.
I was able to find this example which looks rather similar to your code.

There are some differences in parameter handling: they don't have any **kwargs (I don't understand what that is doing in your code), and the __init__ of derived classes calls super().__init__(...) which seems quite logical.

Hope this helps.
Ahmad Qassym 8-Jul-20 12:16pm
without **kwargs you have to modify the subclasses whenever u add parameters to your superclass.
but with **kwargs every subclass doesnt have to care about the inherited attributes ,rather it cares only about its own attributes.

1 solution

You declare your Circle constructor as
def __init__(self,angle:float=180,**kwargs):

but then try to pass values instead of keyword pairs. You need to call it by:

And the same with your Rectangle. Although with so few parameters it hardly seems worth the overhead.

See 4. More Control Flow Tools — Python 3.7.8 documentation[^].
Maciej Los 8-Jul-20 3:52am
Richard MacCutchan 8-Jul-20 4:13am
Thanks again, Maciej. The ability to read the documentation gets me more and more points. :)
Luc Pattyn 8-Jul-20 10:00am
great link
Richard MacCutchan 8-Jul-20 10:05am
Thanks Luc. If only all newbies would make use of the official online tutorials, instead of trying to learn the language through YouTube.
Luc Pattyn 8-Jul-20 10:17am
What do you mean by "YouTube"?
Oh, is that the teen version of "TikTok"?

Actually a parrot clip would fit well on the page you referred to...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900