using classes

Pieter van Oostrum pieter-l at vanoostrum.org
Fri Mar 13 05:10:05 EDT 2020


joseph pareti wrote:

 > 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.

No, the 'self' in the definition of init is the object being initialised. It is supplied by the class code that creates the new instance, not something you provide yourself. Your arguments are bez, ge, ins.

class PKW(Fahrzeug):
    def __init__(self, bez, ge, ins):

-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]


More information about the Python-list mailing list