I'm trying to learn python syntax and I don't understand why the example below doesn't work. I get this error:
TypeError: __init__() takes 1 positional argument but 2 were given
Code:
class Parent:
def __init__(self):
self.lastName = "Mustermann"
self.firstName = "Max"
def get_name(self):
return self.firstName+" "+self.lastName
class Child(Parent):
def __init__(self):
self.firstName = "Moritz"
self.lastName=Parent.lastName
p=Parent()
c = Child(p)
print(c.get_name())
I also don't understand why Parent.lastName should work (according to what I read). Parent is a class, so why would it access the lastName of the instance? What I really want is that Child inherits the lastName of Parent, but not the firstName.
c = Child(p)=>c = Child(). You don't need an instance to inherit from a classChildinstance has aParent, rather than being aParent, in which caseChildshould not inherit fromParent, but insteadChild.__init__should take an instance ofParentas an argument.Parent.lastNamedoesn't work -AttributeError: type object 'Parent' has no attribute 'lastName'but it is stopping at your first error.