'TypeError: missing 1 required positional argument in call to super.__init__()
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__(Circle)
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())
Solution 1:[1]
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)
Solution 2:[2]
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)
Solution 3:[3]
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())
Solution 4:[4]
#The right code I am referring
class Circle:
def __init__(self, radius):
self.radius = radius
class Cylinder(Circle):
def __init__(self, radius, height):
super().__init__(radius)
self.height = height
self.volume = 3.14 * (self.radius ** 2) * self.height
def set_volume(self):
print("Cylinder volume:",self.volume)
radius = float(input("radius:"))
height = float(input("height:"))
cyl = Cylinder(radius, height)
cyl.set_volume()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BrokenBenchmark |
| Solution 2 | Franz Kurt |
| Solution 3 | |
| Solution 4 | Mitchel |
