I have a simple code like below where the class Employee should inherit from the class Person.
class Person:
def __init__(self, firstname, lastname, age):
self.firstname = firstname
self.lastname = lastname
self.age = age
def getname(self):
return self.firstname, self.lastname
def getage(self):
return self.age
class Employee(Person):
def __init__(self, first, last, empid, age):
Person.__init__(self, first, last, age):
self.empid = empid
def getemp(self):
return self.getname() + ", " + self.empid
employee = Employee("Bart", "Simpson", 1006, 16)
print(employee.getemp())
It gives me this error:
File "/tmp/pyadv.py", line 156
Person.__init__(self, first, last, age):
^
SyntaxError: invalid syntax
I checked Python documentation about classes and it didn’t have that superclass initialization inside the __init__()
of the subclass. But I found that in other websites like here, where Dog inherits from Pet.
So, what am I missing?
You're not missing anything. You need to get rid of the :
on that line.
:
only comes after the original definition ie: def getname(self):
and is always followed by an indented line that declares the function. When you are calling a function you don't do this.