简体   繁体   中英

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

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.

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM