How to have different input types for the same function?
Asked Answered
D

3

8

The basic idea of what I want to do is:

def aFunction(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 aFunction(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.

Demetra answered 10/2, 2012 at 9:41 Comment(0)
L
5

Type checks should be avoided in Python (search about "duck typing" on that). When they are necessary, it should usually be done with isinstance rather than an equality check on the type like the question shows. This has the advantage of being more flexible for inheritance situations.

Since Python 3.4, the string-like branch and the dict-like branch can be written in separate functions using stdlib functools.singledispatch.

So instead of:

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

You can now have:

from functools import singledispatch

@singledispatch
def aFunction(input_):
    pass

@aFunction.register(str)
def _(input_):
    print("you gave a string-like input")

@aFunction.register(dict)
def _(input_):
    print("you gave a dict-like input")

In Python 3.5+ you have another option of using type hinting function annotations. Read PEP 484 - Type Hints for more details about that feature. It means the single dispatch generic function above can be written as:

from functools import singledispatch

@singledispatch
def aFunction(input_):
    pass

@aFunction.register
def _(input_: str):
    print("you gave a string-like input")

@aFunction.register
def _(input_: dict):
    print("you gave a dict-like input")
Lovelorn answered 10/2, 2012 at 10:24 Comment(1)
+1 Good point about language philosophy. Plus I believe you should change the name of the input argument (now it overwrites a built-in function).Agapanthus
C
8

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.

Consistent answered 10/2, 2012 at 9:47 Comment(2)
Thanks, I didn't know the name (was looking at polymorphism) that should helpDemetra
Nitpick: multiple dispatch is when there are multiple arguments. This is single dispatch.Lovelorn
L
5

Type checks should be avoided in Python (search about "duck typing" on that). When they are necessary, it should usually be done with isinstance rather than an equality check on the type like the question shows. This has the advantage of being more flexible for inheritance situations.

Since Python 3.4, the string-like branch and the dict-like branch can be written in separate functions using stdlib functools.singledispatch.

So instead of:

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

You can now have:

from functools import singledispatch

@singledispatch
def aFunction(input_):
    pass

@aFunction.register(str)
def _(input_):
    print("you gave a string-like input")

@aFunction.register(dict)
def _(input_):
    print("you gave a dict-like input")

In Python 3.5+ you have another option of using type hinting function annotations. Read PEP 484 - Type Hints for more details about that feature. It means the single dispatch generic function above can be written as:

from functools import singledispatch

@singledispatch
def aFunction(input_):
    pass

@aFunction.register
def _(input_: str):
    print("you gave a string-like input")

@aFunction.register
def _(input_: dict):
    print("you gave a dict-like input")
Lovelorn answered 10/2, 2012 at 10:24 Comment(1)
+1 Good point about language philosophy. Plus I believe you should change the name of the input argument (now it overwrites a built-in function).Agapanthus
D
-1

What you have basically works, there's just some trouble with the variable declarations. Here is a working vesrion:

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

str = 'test'
dicti = dict([('test', 'test')])
aFunction(str = str)
aFunction(dicti = dicti)
aFunction(str = str, dicti = dicti)
aFunction(dicti = dicti, str = str)

puts:

you gave string as input
you gave a dict as input
you gave string as input
you gave a dict as input
you gave string as input
you gave a dict as input

You can also have non-specific arguments that are sent to the function as both a list and a dictionary (see What is a clean, pythonic way to have multiple constructors in Python? for example).

Dracaena answered 10/2, 2012 at 9:58 Comment(2)
Your answer is wrong. You can still call aFunction with: aFunction([1,2,3]) and it will be assuming that you gave it a string (which clearly isn't the case) as well as aFunction(dicti="this is not a dict") and it would assume you inputed a dict. @Lovelorn got the correct answer, using duck typing. Assigning a default argument to a given type, won't give you guarantees, since Python is a weakly typed language and it may change at runtime.Gunzburg
Python is strongly typed; you generally cannot substitute a value of one type when a different type is expected. (If Python were weakly typed, then something like 1 + "1" would equal either 2 or "11", depending on how + was defined.) The fact that you can assign a value of any type to any name makes it dynamically (as opposed to statically) typed.Tithe

© 2022 - 2024 — McMap. All rights reserved.