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.
Type checks should be avoided in Python (search about "duck typing" on that). When they are necessary, it should usually be done with
isinstancerather 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:
You can now have:
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: