Getting an error when my class contains two __init__ method with default and more parameters

Viewed 44

I defined two __init__ methods inside a class as I mentioned in the code snippet

class Employee:
   def __init__(self):
      print('This is init method')

   def __init__(self, workingHrs):
      print('This is init method with parameter')

Now when we use this class

employee = Employee()
employee = Employee(1)

It gives this error:

Traceback (most recent call last):
File, line 20, in <module>
employee = Employee()
TypeError: __init__() missing 1 required positional argument: 'workingHrs'

My question is, how can I use both __init__ methods with and without parameter except for the self parameter.

1 Answers
Related