using classes
Barry
barry at barrys-emacs.org
Thu Mar 12 12:27:59 EDT 2020
> On 12 Mar 2020, at 14:28, joseph pareti <joepareti54 at gmail.com> wrote:
>
> thank you, that fixes it. I also noticed that both statements work:
>
> super(PKW, self).__init__(bez,ge)
This form is for python 2 compatibility.
>
> or
>
> super().__init__(bez,ge)
This is the python 3 way. If you do not have to care about python 2 then this is the form to use.
Barry
>
>> Am Do., 12. März 2020 um 12:58 Uhr schrieb MRAB <python at mrabarnett.plus.com
>>> :
>>
>>> On 2020-03-12 10:54, joseph pareti wrote:
>>> The following code that uses a class 'Fahrzeug' and an inherited class
>>> 'PKW' runs correctly. However, if I use the 'super ' statement in in the
>>> PKW class, it ends with the following error message:
>>>
>>>
>>>
>>> *Traceback (most recent call last): File "erben_a.py", line 19, in
>>> <module> fiat = PKW("Fiat Marea",50,0) File "erben_a.py", line 11, in
>>> __init__ super(PKW, self).__init__()TypeError: __init__() missing 2
>>> required positional arguments: 'bez' and 'ge'*
>>>
>>> ----- CODE THAT WORKS -------
>>> 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):
>>> Fahrzeug.__init__(self, 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)
>>> fiat.einsteigen(3)
>>> fiat.aussteigen(1)
>>> fiat.beschleunigen(10)
>>> print(fiat)
>>>
>>> -----CODE THAT FAILS --------------
>>> 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__()*
>>> 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)
>>> fiat.einsteigen(3)
>>> fiat.aussteigen(1)
>>> fiat.beschleunigen(10)
>>> print(fiat)
>>>
>>>
>> The traceback tells you what the problem is. The line should be:
>>
>> super(PKW, self).__init__(bez, ge)
>> --
>> 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
> --
> https://mail.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list