1

I have a requirement in my application where I need to call a method with same name, but with different number arguments and implementations. Also, I do not want to add any conditional statements which check the arguments received and execute the required implementation. This would have been straight-forward with languages like Java, C++ where function overloading is possible.

I looked into python packages like https://github.com/mrocklin/multipledispatch/blob/master/multipledispatch/ which provide this support, but it seems to be an overhead for the application to depend on such external libraries for the use-case.

Following is what I'm trying to achieve:

def foo(x):
   do_something

def foo(x,y):
   do_something_else

Without doing the following:

def foo(x, y=None):
    if not y:
         do_something
    else:
         do_something_else
  • You can have default argument for the function. – Austin Aug 08 '19 at 10:52
  • This will require to check the value of default argument, which is what I'm trying to avoid. – Abhinav Gupta Aug 08 '19 at 10:59
  • You might also add the requirement that you don't trust packages that are in PyPI for production work because, for example in the second example in your list, @Andriy Drozdyuk claims that 'multimethod' isn't thread-safe. – berniethejet Sep 25 '19 at 19:59

0 Answers0