为什么python不使用类似函数(,, x)的语法作为默认参数? [英] Why python doesn't use syntax like function(,,x) for default parameters?

查看:72
本文介绍了为什么python不使用类似函数(,, x)的语法作为默认参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是,当默认参数

可以处于任何位置时非常方便,例如

def a_func(x = 2,y = 1,z ):

...

(默认值必须最后才是真正的C ++怪癖,重载解析需要
,isn''是吗?)


并且在打电话时,只需要省略参数

使用默认值:

a_func(,,, 3)


通常情况下,一个函数具有独立的

参数,所有参数都有合理的默认值,我想要

提供其中几个。事实上,我可以使用

关键字参数来完成它,但它相当长,你必须记住/查找参数的名称。


python语法中是否存在一些矛盾,它们不允许使用这个功能轻松实现这个功能,或者只是没有人打扰这个?b $ b?如果是前者,请告诉我原因,因为

我非常需要嵌入式python应用程序中的这个功能(对于

与使用这种语法的其他语言的兼容性)

冒险自己实施它,所以不要浪费时间

如果它会破坏某些东西。

或者也许这可能是改进提案的一个想法吗?

I mean, it''s very convenient when default parameters
can be in any position, like
def a_func(x = 2, y = 1, z):
...
(that defaults must go last is really a C++ quirk which
is needed for overload resolution, isn''t it?)

and when calling, just omit parameter when you want to
use defaults:
a_func(, , 3)

There are often situations when a function has independent
parameters, all having reasonable defaults, and I want to
provide just several of them. In fact, I can do it using
keyword parameters, but it''s rather long and you have to
remember/lookup names of parameters.

Is there some contradiction in python syntax which disallows
an easy implementation of this feature, or just nobody bothered
with this? If former is the case, please show me why, because
I badly need this feature in embedded python app (for
compatibility with other language that uses such syntax) and might
venture to implement it myself, so don''t want to waste time
if it''s gonna break something.
Or maybe it might be an idea for enhancement proposal?

推荐答案

Dmitry Anikin写道:
Dmitry Anikin wrote:
python语法中是否存在一些矛盾,它们不允许轻松实现此功能,或者只是没有人为此烦恼?如果是前者,请告诉我原因,因为我非常需要嵌入式python应用程序中的这个功能(与其他使用这种语法的语言兼容)并且可能会冒险实现它我自己,所以不要浪费时间
如果它会破坏某些东西。
Is there some contradiction in python syntax which disallows
an easy implementation of this feature, or just nobody bothered
with this? If former is the case, please show me why, because
I badly need this feature in embedded python app (for
compatibility with other language that uses such syntax) and might
venture to implement it myself, so don''t want to waste time
if it''s gonna break something.



我认为你会觉得很难在Python中实现它。

当然你可以轻松做的就是发明一个神奇的''缺失''值和

映射Python调用的:


a_func(缺失,丢失,3)


拨打电话:


a_func(,, 3)
<用你的另一种语言
。在我看来,虽然写得很好:


a_func(z = 3)


对于阅读代码的人来说应该更清楚,特别是当你来了

在6个月的时间里回到你的代码并找到:


b_func(,,,,,,,,,,, 1 ,,, ,2)

记住Python是一种动态语言,因此编译器无法分辨你实际调用的是什么
函数。在静态绑定的语言参数中,

默认值通常限制为常量值,默认值为

,只需插入代替任何缺少的参数即可调用

汇编。这在Python中是不可能的,因此当您实际进行调用时,解释器必须插入

缺失值。这样做的代码是

足够复杂的当前限制。


为了允许任意参数默认,我认为你必须添加

a表示缺少参数的新魔术值,使编译器传递

代替任何省略的位置参数,然后进行

函数调用代码检查所有参数以查看它们是否缺少值

,如果是,则替换实际默认值。这是有可能的,但它没有真正给你任何额外的力量,因为你已经可以做到这一点

在你需要它的情况下使用关键字参数或通过传递

显式值来表示''用其他一些罐装值替换此参数'。


I think you would find it hard to implement exactly that in Python. Of
course what you can do easily enough is invent a magic ''missing'' value and
map Python calls of:

a_func(missing, missing, 3)

to a call of:

a_func(,,3)

in your other language. It seems to me though that writing:

a_func(z=3)

ought to be clearer to anyone reading the code, especially when you come
back to your code in 6 months time and find:

b_func(,,,,,,,,,,,1,,,,2)
Remember Python is a dynamic language, so the compiler cannot tell which
function you are actually calling. In a statically bound language parameter
defaults are often restricted to constant values and the default values are
simply inserted in place of any missing arguments when the call is
compiled. That isn''t possible in Python, so the interpreter has to insert
the missing values when you actually make the call. The code to do that is
complex enough with the current restrictions.

To allow arbitrary arguments to be defaulted, I think you would have to add
a new magic value to represent a missing parameter, make the compiler pass
that in place of any omitted positional parameters, and then make the
function call code check all parameters to see if they are missing values
and if so substitute the actual default. It would be possible, but it
doesn''t really give you any additional power since you can already do that
in the cases where you need it by using keyword arguments or by passing an
explicit value to mean ''replace this parameter by some other canned value''.


2006年3月10日09: 51:01 GMT

Duncan Booth< du ********** @ invalid.invalid>写道:
On 10 Mar 2006 09:51:01 GMT
Duncan Booth <du**********@invalid.invalid> wrote:
Dmitry Anikin写道:
Dmitry Anikin wrote:
python语法中是否存在一些矛盾?
不允许轻松实现此功能,或者只是没有人烦恼有了这个?如果是前者,请告诉我原因,因为我非常需要嵌入式python应用程序中的这个功能(为了与使用这种语法的其他语言兼容)并且可能冒险到
自己实现它,所以不要浪费时间,如果它会破坏某些东西。
Is there some contradiction in python syntax which
disallows an easy implementation of this feature, or
just nobody bothered with this? If former is the case,
please show me why, because I badly need this feature in
embedded python app (for compatibility with other
language that uses such syntax) and might venture to
implement it myself, so don''t want to waste time if it''s
gonna break something.


我认为你会发现很难真正实现它
在Python中。当然,你可以轻松做到的是发明一个神奇的''缺失''值并映射Python的调用:

a_func(缺失,缺失,3)

a_func(,,)


I think you would find it hard to implement exactly that
in Python. Of course what you can do easily enough is
invent a magic ''missing'' value and map Python calls of:

a_func(missing, missing, 3)

to a call of:

a_func(,,3)




当然,使用并不常见 ;无"为此

目的。我有很多代码可以做类似的事情:


def myfunc(a,b,c):

如果a是None:

a = []

...

-

Terry Hancock(ha*****@AnansiSpaceworks.com)

Anansi Spaceworks http://www.AnansiSpaceworks.com



It''s not uncommon, of course, to use "None" for this
purpose. I have a lot of code that does something like:

def myfunc(a, b, c):
if a is None:
a = []
...
--
Terry Hancock (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com


" Dmitry Anikin" <一个***************** @ vstu.ru>写道:
"Dmitry Anikin" <an*****************@vstu.ru> wrote:
通常情况下,一个函数具有独立的参数,都具有合理的默认值,我想提供其中的几个。事实上,我可以使用
关键字参数来完成它,但它相当长,你必须记住/查找参数名称。


指定关键字参数的名称会让你有点打字

一次,但是在以后为每个人(包括你自己)节省了很多的悲伤>
你试图弄清楚你的代码在6个月之后发生了什么。

我非常需要嵌入式python应用程序中的这个功能(用于与其他语言的兼容性)使用这样的语法)


你能告诉我们更多关于你想要做什么的事吗?

或许它可能是一个想法对于增强建议?
There are often situations when a function has independent
parameters, all having reasonable defaults, and I want to
provide just several of them. In fact, I can do it using
keyword parameters, but it''s rather long and you have to
remember/lookup names of parameters.
Specifying the names of the keyword parameters costs you a little typing
once, but saves everybody (including yourself) a lot of grief later when
you''re trying to figure out what the heck your code does 6 months later.
I badly need this feature in embedded python app (for
compatibility with other language that uses such syntax)
Can you tell us more about what it is that you''re trying to do?
Or maybe it might be an idea for enhancement proposal?




你总是可以写一个PEP,但说实话,这听起来不像是

会受到社区的热烈欢迎。



You can always write up a PEP, but to be honest, this doesn''t sound like
one that would meet with much enthusiasm from the community.


这篇关于为什么python不使用类似函数(,, x)的语法作为默认参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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