Python inheritance - error

I am getting err “TypeError: init() takes 3 positional arguments but 4 were given” while using below code block (1st snippet).
Where as the 2nd block working fine. Not able to find out what went wrong. Need suggestion…

========== Snippet 1 ===========
class Person:                       
  def __init__(self, name, age):
    self.fname = name
    self.curage = age
class Student2(Person):             
  def _init_(self, name, age, salary):
    Person.__init__(self, name, age)
    self.csalary = salary

  def cprint(self):
    print("Welcome", self.fname, "aged", self.curage, "in our company with a salary of", self.csalary)
y = Student2("Andrew", 32, 1000)
========== Snippet 2 ===========
class Pers:                         
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def persWel(self):
    print(self. firstname, " ", self.lastname)

class Stu(Pers):                    
  def __init__(self, fname, lname, year):       
    Pers.__init__(self, fname, lname)
    self.graduationyear = year

  def welcome(self):
    print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)

x = Stu("Mike", "Olsen", 2019)

Note: Some of the syntax is getting lost while I submit the code snippet… like the preceding and trailing underscores around init.


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


1 Like

Hello Soumen,
Please check the underscore in your init definition for student2, it seems that there are some underscores missing.
I have added the underscore and ii seems to be working fine,
“Welcome Andrew aged 32 in our company with a salary of 1000”
maybe in your editor you shoud delete and retype the underscore.

Thank you so much… Believe I need a better Editor. Default IDLE editor does not show these kind of differences. Thanks