Reputation: 83
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()
Upvotes: 0
Views: 149
Reputation: 1125088
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
Upvotes: 1