Derek Zuurmond
Derek Zuurmond

Reputation: 27

_init__() takes 1 positional argument but 2 were given

I'm testing some code for a course OOP, but I run into a problem. I am programming a circle and a cylinder, with the circle class also in the init of the cylinder. I have 2 arguments for the cylinder, but when I give 2 arguments, it's said that I only need 1 and if I give one argument than it gaves the output one is missing.

with the variable a it works, but the error is in variable b. What do I wrong

import math

class CCircle:
    def __init__(self):
        self._radius = 0
    @property
    def area(self):
        return self._radius**2 * math.pi
    @area.setter
    def area(self, value):
        self._radius = math.sqrt(value / math.pi)
    @property
    def circumference(self):
        return self._radius * 2 * math.pi
    @circumference.setter
    def circumference(self, value):
        self._radius = value / (2 * math.pi)

class CCylinder:
    def __init__(self, radius, height):
        self._circle = CCircle(radius)
        self._height = height
    @property
    def circumference(self):
        return self._circle.circumference
    @property
    def ground_area(self):
        return self._circle.area
    @property
    def total_area(self):
        return self._circle.area + self._height * self._circle.circumference
    @property
    def volume(self):
        return self._circle.area * self._height


a = CCircle()
b = CCylinder(1,4)

init() takes 1 positional argument but 2 were given

Upvotes: 1

Views: 2162

Answers (3)

Srinivas Kumar
Srinivas Kumar

Reputation: 49

You might have had a package folder locally at the place where the .py file is , delete that and that should solve your issue

Upvotes: 0

Tané Tachyon
Tané Tachyon

Reputation: 1112

You should have your CCircle class start like this

class CCircle:
    def __init__(self, radius=0):
        self._radius = radius

so that you get the default radius of 0 that you seem to want, but can also initialize it with a radius value like you're doing in the init code of your CCylinder class.

Upvotes: 2

Simon Crane
Simon Crane

Reputation: 2182

The problem is with this line:

self._circle = CCircle(radius)

but __init__ for the CCircle class does not take any arguments (except for self) so this is causing the error.

Upvotes: 2

Related Questions