Python doesn't do overloading like Java or C, because Python is a dynamically typed language. And we can just use *args instead.
Java style:
class DisplayOverloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c + " "+num);
}
}
but python call second Print method when call it:
class Dog:
def __init__(self,age,name):
self.age=age
self.name=name
def Print(self,age):
print(self.age)
def Print(self,name,age):
print(self.name)
d=Dog(5,'dobi')
d.Print('dobi',5)#dobi
d.Print(5) #Error !
actually second Print replace to first Print.
But there is something strange here about the type function, it does different things when passed in a different number of arguments:
Return the object type:
>>> x=12
>>> y=str()
>>>
>>> type(x)
<class 'int'>
>>> type(y)
<class 'str'>
or make a class:
>>> klass=type('MyClass',(),{})
>>> klass
<class '__main__.MyClass'>
Is this overloading? If so, why can't we use overloading in other methods?