Python的函数重载习惯用法 [英] Python's idiom for function overloads

查看:96
本文介绍了Python的函数重载习惯用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



您好,


由于Python没有静态输入,与传统相同的结果如何?
功能超载导致实现?随着函数重载,

根据数据类型选择代码路径。是透明和自动的

因为打字系统弄清楚到底是什么。


但是在Python中,当一个人希望能够传递不同的数据类型时进入

单一入口点对于功能,如何最好地完成?要在

函数中执行带有type()函数的if语句?

干杯,


Frans

解决方案

您好Frans,

由于Python没有静态类型,如何相同结果传统的
函数重载导致实现?


显然动态打字。 :-)


您无法重现C ++过载习惯用法,但您可以通过手动类型测试获得

关闭。

要在
函数中使用type()函数执行if语句吗?




我不知道任何其他方法。


def a(arg1):

如果type(arg1)== types.IntType:return aWithInt(arg1)

if type(arg1)= = types.ListType:返回aWithList(arg1)

...


如你所见,有时它有点乏味。


如果你想要使用完全不同的签名,你必须使用可变参数列表来玩
。但我发现根据我的经验

,接近C ++成语的最佳方法,同时提高

可读性,是通过使用kwparams:


def a(** kwparams):

如果kwparams.has_key(''argInt''):aWithInt(kwparams [''argInt''])

if kwparams.has_key(''argString''):aWithString(kwparams [''argString''])


解析代码是相同的,但意图是用户更好

表示你​​可以更好地捕捉滥用:

如果kwparams.has_key(''argInt'')和kwparams.has_key(''argString' '):

print"你这个笨蛋,一个只能用于字符串或int但不能同时使用

!"

sys.exit(1)


这个例子在实际案例中说得更好。想象一下processPixmap函数:

processPixmap(pixmap = QPixmap(...))

processPixmap(filename =''my_pixmap.png'')

processPixmap(buffer = my_pixmap_string_content)


多个参数实际上效果更好。


问候,

Philippe


Frans Englich写道:

但是在Python中,当人们希望能够将不同的数据类型传递到
单个入口点对于功能,如何最好地完成?要在
函数中使用type()函数做一个if语句吗?




它通常在很大程度上取决于具体的用例...你呢有一个

特定的例子吗?


史蒂夫


> >由于Python没有静态类型,如何得到相同的结果

传统的

函数重载会导致实现吗?


你在Python中编程越多,你就越少想念它。


正如Philippe所说,使用支持协议的对象或者决定

检查其类型后如何处理它。我这样做,如果必须的话,

是这样的:


def doIt(arg):

if type(arg) )== type([]):

map(doIt,arg)

else:

#在标量类型上执行

#...

返回结果


HTH

Franz GEIGER

Philippe Fremy < pH值** @ freehackers.org> schrieb im Newsbeitrag

news:41 ********************** @ news.free.fr ...

你好Frans,

由于Python没有静态类型,与


传统函数重载相同的结果如何实现?



显然有动态打字。 :-)

你无法重现C ++重载习惯用法,但你可以通过手动类型测试得到一些东西

>在
> function使用type()函数执行if语句?



我不知道任何其他方法。

def a(arg1):
如果type(arg1)== types.IntType:return aWithInt(arg1)
如果type(arg1)== types.ListType:return aWithList(arg1)
...

如你所见,有时候有点乏味。

如果你想要完全不同的签名,你必须使用可变参数列表。但我发现,根据我的经验,接近C ++成语,同时提高可读性的最佳方法是使用kwparams:

def a(** kwparams ):
如果kwparams.has_key(''argInt''):aWithInt(kwparams [''argInt''])
如果kwparams.has_key(''argString''):aWithString(kwparams [' 'argString''])

解析代码是相同的,但用户的意图更好地表达,你可以更好地捕捉滥用:
如果kwparams .has_key(''argInt'')和kwparams.has_key(''argString''):
打印你是愚蠢的白痴,一个只能用于字符串或int而不是
两者都在同时!
sys.exit(1)

这个例子在真实案例中说得更好。想象一下processPixmap



函数:processPixmap(pixmap = QPixmap(...))
processPixmap(filename =''my_pixmap.png'')
processPixmap( buffer = my_pixmap_string_content)

多个参数实际上效果更好。

问候,

Philippe




Hello,

Since Python doesn''t have static typing, how is the same result as traditional
function overloads results in acheived? With function overloads the
"selection of code path depending on data type" is transparent and automatic
since the typing system figure out what goes to what.

But in Python, when one wants to be able to pass different data types into a
single "entry point" for functionality, how is that best done? To in a
function do an if statement with the type() function?
Cheers,

Frans

解决方案

Hi Frans,

Since Python doesn''t have static typing, how is the same result as traditional
function overloads results in acheived?
With dynamic typing obviously. :-)

You can not reproduce the C++ overload idiom but you can get something
close with manual type testing.
To in a
function do an if statement with the type() function?



I am not aware of any other method.

def a( arg1 ):
if type(arg1) == types.IntType: return aWithInt(arg1)
if type(arg1) == types.ListType: return aWithList(arg1)
...

As you see, it is a bit tedious sometimes.

If you want to juggle with completely different signatures, you have to
play with variable argument lists. But I have found in my experience
that the best way to get close to the C++ idiom, while improving
readbility, is by using kwparams:

def a(**kwparams):
if kwparams.has_key(''argInt''): aWithInt(kwparams[''argInt''])
if kwparams.has_key(''argString''): aWithString(kwparams[''argString''])

The parsing code is the same, but the intent of the user is better
expressed and you can catch misuse in a better fashion:
if kwparams.has_key(''argInt'') and kwparams.has_key(''argString''):
print "You stupid moron, a can be used only with string or int but not
both at the same time!"
sys.exit(1)

The example speaks better in a real case. Imagine a processPixmap function:
processPixmap( pixmap=QPixmap(...) )
processPixmap( filename=''my_pixmap.png'' )
processPixmap( buffer=my_pixmap_string_content )

It works actually even better with multiple arguments.

regards,

Philippe


Frans Englich wrote:

But in Python, when one wants to be able to pass different data types into a
single "entry point" for functionality, how is that best done? To in a
function do an if statement with the type() function?



It often depends a lot on the specific use case... Do you have a
particular example in mind?

Steve


> > Since Python doesn''t have static typing, how is the same result as
traditional

function overloads results in acheived?
The more you program in Python, the less you are missing it.

As Philippe already said, use objects that support the protocol or decide
what to do with it after having checked its type. I do that, if I have to,
like so:

def doIt(arg):
if type(arg) == type([]):
map(doIt, arg)
else:
# Do it on a scalar type
# ...
return result

HTH
Franz GEIGER
"Philippe Fremy" <ph**@freehackers.org> schrieb im Newsbeitrag
news:41**********************@news.free.fr...

Hi Frans,

Since Python doesn''t have static typing, how is the same result as

traditional function overloads results in acheived?



With dynamic typing obviously. :-)

You can not reproduce the C++ overload idiom but you can get something
close with manual type testing.

> To in a
> function do an if statement with the type() function?



I am not aware of any other method.

def a( arg1 ):
if type(arg1) == types.IntType: return aWithInt(arg1)
if type(arg1) == types.ListType: return aWithList(arg1)
...

As you see, it is a bit tedious sometimes.

If you want to juggle with completely different signatures, you have to
play with variable argument lists. But I have found in my experience
that the best way to get close to the C++ idiom, while improving
readbility, is by using kwparams:

def a(**kwparams):
if kwparams.has_key(''argInt''): aWithInt(kwparams[''argInt''])
if kwparams.has_key(''argString''): aWithString(kwparams[''argString''])

The parsing code is the same, but the intent of the user is better
expressed and you can catch misuse in a better fashion:
if kwparams.has_key(''argInt'') and kwparams.has_key(''argString''):
print "You stupid moron, a can be used only with string or int but not
both at the same time!"
sys.exit(1)

The example speaks better in a real case. Imagine a processPixmap


function: processPixmap( pixmap=QPixmap(...) )
processPixmap( filename=''my_pixmap.png'' )
processPixmap( buffer=my_pixmap_string_content )

It works actually even better with multiple arguments.

regards,

Philippe



这篇关于Python的函数重载习惯用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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