如何为同一个函数使用不同的输入类型? [英] How to have different input types for the same function?

查看:125
本文介绍了如何为同一个函数使用不同的输入类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的基本想法是:
$ b $ pre $ def cFuncion(string ='',dicti = {} ):
如果len(str)> 0:
print'你给字符串作为输入'
if len(dicti)> 0:
print'你输入了一个字典'

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

我知道这种想法在更多OO类型的语言中是可能的,但是也可以用python?



现在我正在做

pre code $> def aFuncion(input):
if type(input)== str:
print'you give string as input'
if type(input)== dict:
print'你输入了一个字典'

功能('test')

但是我希望当函数被调用时清晰的区别是清楚的。 解决方案

这个想要功能被称为不能很好地与语言的设计理念擦擦。谷歌鸭子打字的更多信息。接受的输入应该由函数的文档字符串来表示,这就是你需要做的。

在Python中,当你希望你的输入是一个字符串或字典时,那么你只需编写代码,假设输入将是一个行为表现为类似字符串的方式或类似字典的方式。如果输入不是,那么你可以尝试处理,如果你想要的话,但通常你可以简单地让你的代码简单地弹出一个未处理的异常。这让球回到了主叫方的法庭,要么决定如何处理这种情况,要么意识到他们发送的数据不好。

类型检查通常应该避免,如果真的有必要,应该使用 isinstance 来完成,而不是相等检查就像你所做的那样。这具有在继承情况下更灵活的优点。

  def aFuncion(input_):
如果isinstance(input_,str):
print'you gave'一个类似字符串的输入'
elif isinstance(input_,dict):
print'你给出了一个字典式的输入'

aFunction('test')

在python3中,您现在可以使用另一种使用类型提示功能注释的选项。有关该功能的更多详细信息,请阅读 PEP 484

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

解决方案

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.

这篇关于如何为同一个函数使用不同的输入类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆