Overloading a method inside a class in Python


All my life I have been writing in Java, I started to study python, and in connection with this, many questions arose. For example, in Java you can write like this:

public class test {
    public static void main(String[] args) {


    private void testrunner()


    private void testrunner(int i)


    private void testrunner(String c)


How to write the same in python? Well, I write like this IDE immediately starts swearing

class test:
def testrunner(self):
def testrunner(self,a):


There is no way in Python to overload a class method like in Java or C.

But there is a crutch. The method can have default parameter values, which together with the type checking of the argument will allow you to do what you want:

from types import *

class Test(object):

    def testrunner(self, i=None):
        if isinstance(i, str):     
            print 'c: ', i
        elif isinstance(i, int):
            print 'b: ', i
            print 'a' 
Scroll to Top