Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have different input types for the same function?

The basic idea of what I want to do is:

def aFuncion(string = '', dicti = {}):
    if len(str) > 0:
         print 'you gave string as input'
    if len(dicti) > 0:
         print 'you gave a dict as input'

aFunction(string = 'test')
dict['test'] = test
aFunction(dicti = dict)

I know this kind of idea is possible in more OO type of languages, but is this also possible in python?

Right now I'm doing

def aFuncion(input):
    if type(input) == str:
         print 'you gave string as input'
    if type(input) == dict:
         print 'you gave a dict as input'

aFunction('test')

But I want the difference to be clear when the function is called

like image 299
Niek de Klein Avatar asked Feb 10 '12 09:02

Niek de Klein


People also ask

How do you take two inputs of a function?

To create a function with two inputs, we just need to provide two different arguments inside function. For example, if we want to create a function to find the square of a+b then we can use x and y inside function.

How do you check for different inputs in Python?

Use string isdigit() method to check user input is number or string. Note: The isdigit() function will work only for positive integer numbers. i.e., if you pass any float number, it will not work. So, It is better to use the first approach.


2 Answers

The idea of having the same method support different argument types is known as multiple dispatch or multimethods.

To get a good introduction to it, you can read this Guido Van Rossum article and have a look at PyPi since there are a few multimethod packages available.

like image 73
jcollado Avatar answered Nov 15 '22 18:11

jcollado


This goal of wanting "the difference to be clear when the function is called" doesn't rub so well with the design philosophy of the language. Google "duck typing" for more information about that. The accepted inputs should be made clear by the docstring of the function, and that's all you need to do.

In python, when you want your input it to be a string or a dict, then you just write code which assumes the input will be an object which behaves in behave in a string-like way or a dict-like way. If the input doesn't, then you can try to handle that gracefully if you want, but usually you can simply leave your code to simply pop with an unhandled exception. This puts the ball back in the caller's court to either decide how to handle the situation, or realise that they were sending in bad data.

Type checks should be avoided in general, if really necessary it should be done with isinstance rather than an equality check on the type like you have done. This has the advantage of being more flexible for inheritance situations.

def aFuncion(input_):
    if isinstance(input_, str):
        print 'you gave a string-like input'
    elif isinstance(input_, dict):
        print 'you gave a dict-like input'

aFunction('test')

In python3 you now have another option of using type hinting function annotations. Read PEP 484 for more details about that feature.

like image 20
wim Avatar answered Nov 15 '22 16:11

wim