using classes
joseph pareti
joepareti54 at gmail.com
Fri Mar 13 04:46:29 EDT 2020
one more question. In the code below, there are 2 init() methods, one for
the class 'Fahrzeug' and one for the class 'PKW'.
The program works when I instantiate the class as:
fiat = PKW("Fiat Marea",50,0)
but it fails if I say:
*fiat = PKW("Fiat Marea",50,0,1)*
*Traceback (most recent call last): File "erben_a.py", line 19, in
<module> fiat = PKW("Fiat Marea",50,0,1)TypeError: __init__() takes 4
positional arguments but 5 were given*
yet the statement in *bold* matches IMO the init() method of the PKW class.
Can anyone explain why? Thanks.
---
class Fahrzeug:
def __init__(self, bez, ge):
self.bezeichnung = bez
self.geschwindigkeit = ge
def beschleunigen(self, wert):
self.geschwindigkeit += wert
def __str__(self):
return self.bezeichnung + " " +str(self.geschwindigkeit) + " km/h"
class PKW(Fahrzeug):
def __init__(self, bez, ge, ins):
super(PKW, self).__init__(bez,ge)
self.insassen = ins
def __str__(self):
return Fahrzeug.__str__(self) + " " + str(self.insassen) + "
Insassen"
def einsteigen(self, anzahl):
self.insassen += anzahl
def aussteigen(self, anzahl):
self.insassen -= anzahl
fiat = PKW("Fiat Marea",50,0,1)
fiat = PKW("Fiat Marea",50,0)
fiat.einsteigen(3)
fiat.aussteigen(1)
fiat.beschleunigen(10)
print(fiat)
Am Do., 12. März 2020 um 17:38 Uhr schrieb Pieter van Oostrum <
pieter-l at vanoostrum.org>:
> joseph pareti <joepareti54 at gmail.com> writes:
>
> > thank you, that fixes it. I also noticed that both statements work:
> >
> > super(PKW, self).__init__(bez,ge)
> >
> > or
> >
> > super().__init__(bez,ge)
>
> The first is the required Python 2 calling (at least the first argument is
> required). The second way can be used in Python 3.
> --
> Pieter van Oostrum
> www: http://pieter.vanoostrum.org/
> PGP key: [8DAE142BE17999C4]
> --
> https://mail.python.org/mailman/listinfo/python-list
>
--
Regards,
Joseph Pareti - Artificial Intelligence consultant
Joseph Pareti's AI Consulting Services
https://www.joepareti54-ai.com/
cell +49 1520 1600 209
cell +39 339 797 0644
More information about the Python-list
mailing list