0

Hey I'm new to this python thing. Have few days to learn all about classes but at the beginning I have problem. I got this kind of error TypeError: __init__() missing 1 required positional argument: 'nazwa'. Can you help me with solution? I want to print out calculation for my object.

class Figura(object):
    def __init__(self,nazwa):
        self.nazwa = nazwa
    def calculate(self):
        print(self.nazwa)

class Kolo(Figura):
    def __init__(self,nazwa,promien):
        Figura.__init__(self)
        self.promien = promien

    def calculate(self):
        Figura.calculate(self)
        print(2 * 3.1415 * promien)

kolo1 = Kolo('kolo',4)
kolo1.calculate()

1 Answer 1

1

You'll need to pass on the nazwa argument in the Kolo.__init__() method call:

class Kolo(Figura):
    def __init__(self, nazwa, promien):
        Figura.__init__(self, nazwa)
        self.promien = promien

You may want to use the super() function there instead and avoid having to repeat the parent class:

class Kolo(Figura):
    def __init__(self, nazwa, promien):
        super().__init__(nazwa)
        self.promien = promien

    def calculate(self):
        super().calculate()
        print(2 * 3.1415 * self.promien)

Note that I corrected your Kolo.calculate() method as well; you want to refer to self.promien rather than make promien a local name.

Demo:

>>> class Figura(object):
...     def __init__(self,nazwa):
...         self.nazwa = nazwa
...     def calculate(self):
...         print(self.nazwa)
...
>>> class Kolo(Figura):
...     def __init__(self, nazwa, promien):
...         super().__init__(nazwa)
...         self.promien = promien
...     def calculate(self):
...         super().calculate()
...         print(2 * 3.1415 * self.promien)
...
>>> kolo1 = Kolo('kolo',4)
>>> kolo1.calculate()
kolo
25.132
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.