0

I got this type of error in runtime

Traceback (most recent call last):   
  File "C:\Python37-32\python\program\overiding.py", line 17, in <module>
    e1=Employee("Rajesh",9000) 
TypeError: object.__new__() takes no parameters
class Employee:
    def _init_(self, nm=None, sal=None):
        self.name=nm
        self.salary=sal
    def getName(self):
        return self.name
    def getSalary(self):
        return self.salary

class SalesOfficer(Employee):
    def _init_(self,nm=None,sal=None,inc=None):
        super()._init_(nm,sal)
        self.incnt=inc
        def getSalary(self):
            return self.salary+self.incnt
Nick T
  • 25,754
  • 12
  • 83
  • 121
  • 1
    `_init_` is incorrect. It should have two underscores: `__init__`. – iz_ May 03 '19 at 02:50
  • @Devesh I think by adding the `class Employee` line you may have masked the error. I suspect something else might be in it's place. – Nick T May 03 '19 at 03:24
  • I have just corrected the indentation, the line `class Employee` was in the same line with triple quotes, which caused the line to not show up, I just placed that line in the next line! and then it showed up! Now you last edit overwrote that @NickT :( – Devesh Kumar Singh May 03 '19 at 03:26
  • @DeveshKumarSingh derp, the revision viewer didn't render it at all. sorry about that https://stackoverflow.com/revisions/55962624/1 – Nick T May 03 '19 at 03:28

1 Answers1

0

The constructor function is named __init__ (with two underscores around init) and not _init_ (just one underscore), just change that and your code will work

class Employee:

    #Fixed this
    def __init__(self, nm=None, sal=None):
            self.name=nm
            self.salary=sal

    def getName(self):
        return self.name

    def getSalary(self):
        return self.salary

class SalesOfficer(Employee):

    # Fixed this
    def __init__(self,nm=None,sal=None,inc=None):
        super()._init_(nm,sal)
        self.incnt=inc

    def getSalary(self):
        return self.salary+self.incnt


e1=Employee("Rajesh",9000)
print(e1.getName())
print(e1.getSalary())

The output will be

Rajesh
9000

A point to note here then when I tried to run OP's original code, I got TypeError: Employee() takes no arguments instead of object.__new__() takes no parameters which the OP gets, but in my opinion, both errors point in the same direction where it says that since no __init__ was defined, and you cannot set any attributes in the constructor!

Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
  • Then what might cause it? I fixed this issue and the code works! – Devesh Kumar Singh May 03 '19 at 02:46
  • Also in my case @wim I get `TypeError: Employee() takes no arguments` instead of `object.__new__() takes no parameters` which the OP gets! I think both errors point in the same direction where it says that since no `__init__` was defined, you cannot set any attributes in the constructor! – Devesh Kumar Singh May 03 '19 at 03:50