python中的bug!?函数中可选参数的持久值! [英] Bug in python!? persistent value of an optional parameter in function!

查看:65
本文介绍了python中的bug!?函数中可选参数的持久值!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助!我发现了一个严重的错误吗?

这对我来说似乎是非常不受欢迎的行为。从下面的程序

,我得到输出:


call1:[''sdf'']

call2:[' '设置在call2的测试中'']

call3:[''设置在call2的测试中'''


而不是我应该得到的,


call1:[''sdf'']

call2:[''设置为call2'''测试''

call3 :[''在call3的测试中设置'''


我正在使用Python 2.4.4c1(#2,2006年10月11日,21:51:02)。代码是

以下。

#!/ usr / bin / python

def testPersistence(anarg,twooption = [] ):

#print anarg

如果不是两个选项:

twooption.append(''测试中为''+ anarg设置)

打印anarg +'':''+ str(twooption)

testPersistence(''call1'',twooption = [''sdf'']);

testPersistence(''call2'');

testPersistence(''call3'');

Help! Have I found a serious bug?
This seems like highly undesired behaviour to me. From the program
below, I get output:

call1: [''sdf'']
call2: [''Set within test for call2'']
call3: [''Set within test for call2'']

instead of what I should get,

call1: [''sdf'']
call2: [''Set within test for call2'']
call3: [''Set within test for call3'']

I''m using Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02). The code is
below.
#!/usr/bin/python

def testPersistence(anarg,twooption=[]):
#print anarg
if not twooption:
twooption.append(''Set within test for ''+anarg)
print anarg +'': ''+str(twooption)

testPersistence(''call1'',twooption=[''sdf'']);
testPersistence(''call2'');
testPersistence(''call3'');

推荐答案

" C Barr Leigh" < cp ******** @ gmail.comwrites:
"C Barr Leigh" <cp********@gmail.comwrites:

帮助!我发现了一个严重的错误吗?

这对我来说似乎是非常不受欢迎的行为。从下面的程序

,我得到输出:
Help! Have I found a serious bug?
This seems like highly undesired behaviour to me. From the program
below, I get output:



这是故意的,而不是bug,请参阅文档。是否可取是

a不同的问题。

It is intentional, not a bug, see the docs. Whether it''s desirable is
a different question.


En Wed,2007年3月7日23:39:21 -0300,C Barr Leigh < cp ******** @ gmail.com>
$ b $bescribió:
En Wed, 07 Mar 2007 23:39:21 -0300, C Barr Leigh <cp********@gmail.com>
escribió:

帮助!我发现了一个严重的错误吗?
Help! Have I found a serious bug?



完全没有!这是设计的。

Not at all! This is by design.


def testPersistence(anarg,twooption = []):

#print anarg

如果不是两个选项:

twooption.append(''测试中为''+ anarg设置)
def testPersistence(anarg,twooption=[]):
#print anarg
if not twooption:
twooption.append(''Set within test for ''+anarg)




< a rel =nofollowhref =http://effbot.org/pyfaq/why-are-default-values-shared-between-objects.htmtarget =_ blank> http://effbot.org/pyfaq /why-are-defa...en-objects.htm


-

Gabriel Genellina

See
http://effbot.org/pyfaq/why-are-defa...en-objects.htm

--
Gabriel Genellina


Paul Rubin写道:
Paul Rubin wrote:

" C Barr Leigh" < cp ******** @ gmail.comwrites:
"C Barr Leigh" <cp********@gmail.comwrites:

>>帮助!我发现了一个严重的错误吗?
这对我来说似乎是非常不受欢迎的行为。从下面的程序
,我得到输出:
>>Help! Have I found a serious bug?
This seems like highly undesired behaviour to me. From the program
below, I get output:




这是故意的,而不是bug,请参阅文档。是否可取是一个不同的问题。




It is intentional, not a bug, see the docs. Whether it''s desirable is
a different question.



True。禁止可变值作为可选参数的初始值是有意义的。目前的行为

很傻。


John Nagle

True. It would make sense to disallow mutable values as
initial values for optional arguments. The present behavior
is silly.

John Nagle


这篇关于python中的bug!?函数中可选参数的持久值!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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