I defined a class in python as following.
class myclass:
def __init__(self,edate,fdate=""):
print("Constructors with default arguments...")
def __init__(self):
print("Default Constructor")
I created an object for this class,
obj = myclass()
It works fine. And I expected the following object creation will work,
obj1 = myclass("01-Feb-2019")
But It throws an error saying,
Traceback (most recent call last):
File "class.py", line 9, in <module>
obj = myclass("01-Feb-2019")
TypeError: __init__() takes 1 positional argument but 2 were given
But If I change the definition of the class as follows,
class myclass:
def __init__(self):
print("Default Constructor")
def __init__(self,edate,fdate=""):
print("Constructors with default arguments...")
Now obj1 = myclass("01-Feb-2019")
works. But obj = myclass()
throws the following error,
Traceback (most recent call last):
File "class.py", line 10, in <module>
obj = myclass()
TypeError: __init__() missing 1 required positional argument: 'edate'
Could we define a constructor overloading in Python? Could I define a constructor which accepts the empty argument as well as one argument?