Python Method Overloading

You might be able to get the code snippet and explanation on the internet easily, but objective of the article is information sharing.


In ideal world of python, it does not support method overloading . it supports method overriding but certainly not method overloading.

In below example we can see that it throws error due to wrong method signature though show() signature is present but it is not able to access due to method overriding .


Error due to Method overriding

?

But there might be requirement where developers need same function name to maintain code standards, but calculation would be different based on requirements. How exactly that can be solved in python. For that python has 3rd party tool called multipledispatch python library.

It comes handy during this scenarios and life saver for developers.

Please find example below:

Successfully executed code with method overloading

In above example using dispatch decorator we are passing param type so that python interpreter will be able to understand which method to invoke based on method signature.


Thanks

Gourab



要查看或添加评论,请登录

Gourab Datta的更多文章

  • Python Object as function parameter

    Python Object as function parameter

    This topic would be pretty simple and easily available in the internet. But trying to bring this concept over here so…

社区洞察

其他会员也浏览了