Home > Software engineering >  TypeError: missing 1 required positional argument in call to super.__init__()
TypeError: missing 1 required positional argument in call to super.__init__()

Time:03-20

I'm writing a program to find the volume of a cylinder using inheritance. However, I'm getting the following error, and I'm not sure how to resolve it:

TypeError: __init__() missing 1 required positional argument: 'height'

Here is my code:

class Circle:
    def __init__(self,radius,height):
        self.radius = radius
        self.height = height
    
class Cylinder(Circle):
    def __init__(self,radius,height):
        super().__init__(radius, height)
        self.volume = 3.14 * (self.radius ** 2) * self.height
   def set_volume(self):
        print(self.volume)
    
radius = float(input("radius:"))
height = float(input("height:"))
cyl = Cylinder(radius,height)
print("Cylinder volume:", cyl.set.volume())

CodePudding user response:

Your call to the __init__ of the superclass isn't correct. You need to pass in the radius and height parameters explicitly, rather than passing in the name of the superclass.

super().__init__(Circle)

should be

super().__init__(radius, height)

CodePudding user response:

I recommend to you study a little bit OOP and relation between classes, because inheritance is not the case here and has no relation with the output error.

First things first and a cilynder shape is a 3d geometric form that can agregate a circle or implements another interface to a circle but is independent.

So this forms are not properly a family and should not have a link by inheritance.

See this reference for more interesting things about agregation and comosition in software development.

I implement a funcional code for you, just a refactor.

class Circle:
    def __init__(self, radius):
        self._radius = radius
        self._area = 3.14 * (self._radius ** 2)
    
    @property
    def radius(self):
        return self._radius 

    @property
    def area(self):
        return self._area
    
class Cylinder:
    def __init__(self, radius, height, circle = None):
        self._height = height
        self._circle = circle if circle else Circle(radius)
        self._volume = self._circle.area * self._height

    @property
    def heigth(self):
        return self._height 

    @property
    def volume(self):
        return self._volume

    @property
    def area(self):
        return self._circle.area
    
# radius = float(input("radius:"))
# height = float(input("height:"))
radius = 5
height = 10

circle = Circle(radius)
cylinder = Cylinder(radius, height, circle = circle)

print("circle area:", circle.radius)
print("Cylinder area:", cylinder.area)
print("Cylinder height:", cylinder.heigth)
print("Cylinder volume:", cylinder.volume)

CodePudding user response:

Since height is a property associated with cylinder we will put height property in the Cylinder class and the base class Circle will only have radius property.
What you could better do have like below

class Circle:
    def __init__(self, radius):
        self.radius = radius
    
class Cylinder(Circle):
    def __init__(self, radius, height):
        super().__init__(radius)
        self.height = height # It makes sense to put height in the cyclinder here 
        self.volume = 3.14 * (self.radius ** 2) * self.height
   def set_volume(self):
        print(self.volume)
    
radius = float(input("radius:"))
height = float(input("height:"))
cyl = Cylinder(radius, height)
print("Cylinder volume:", cyl.set.volume())
  • Related