Reputation: 8834
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.
Upvotes: 8
Views: 17930
Reputation: 363304
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")
Upvotes: 5
Reputation: 191789
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).
Upvotes: -1
Reputation: 40414
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.
Upvotes: 8