def X(l = []):怪异。 Python bug? [英] def X(l=[]): weirdness. Python bug ?
问题描述
大家好。
我偶然发现了一条我根本不懂的蟒蛇行为。
Python 2.5.2(r252:60911,2008年7月31日,17:28:52)
#function
def X(l = []):
l.append(1)
打印l
#第一次打电话给X
X( )
[1]
#秒呼叫X
X()
[1 ,1 $
列表参数l在两个连续的X()调用之间的位置。
为什么不重新创建一个空的列表?
这是正确的行为还是Python错误?
有没有人对语言文档中有任何描述这种行为的指针?
谢谢所有
Bart van Deenen
Hi all.
I''ve stumbled onto a python behavior that I don''t understand at all.
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
# function
def X(l=[]):
l.append(1)
print l
# first call of X
X()
[1]
#second call of X
X()
[1, 1]
Where does the list parameter ''l'' live between the two successive calls of X().
Why is it not recreated with an empty list?
Is this correct behavior or is it a Python bug?
Does anyone have any pointers to the language documentation where this behavior is described?
Thanks all
Bart van Deenen
推荐答案
8月22日,11:13 * am,Bart van Deenen
< b ... @ at.vandeenensupport.punt.com.invalidwrote:
On Aug 22, 11:13*am, Bart van Deenen
<b...@at.vandeenensupport.punt.com.invalidwrote:
大家好。
我偶然发现了一个我根本不理解的python行为。
Python 2.5.2(r252:60911,2008年7月31日,17:28:52)
#function
def X(l = []):
* * l.append(1)
* *打印l
#第一次拨打X
X()
[1]
#秒呼叫X
X()
[1,1]
列表参数l在X()的两次连续调用之间的位置。
为什么是它没有用空列表重新创建?
这是正确的行为还是Python错误?
有没有人有语言文档的指针描述这种行为?
谢谢大家
Bart van Deenen
Hi all.
I''ve stumbled onto a python behavior that I don''t understand at all.
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
# function
def X(l=[]):
* *l.append(1)
* *print l
# first call of X
X()
[1]
#second call of X
X()
[1, 1]
Where does the list parameter ''l'' live between the two successive calls of X().
Why is it not recreated with an empty list?
Is this correct behavior or is it a Python bug?
Does anyone have any pointers to the language documentation where this behavior is described?
Thanks all
Bart van Deenen
http://docs.python.org/ref/function.html
默认参数值是eva功能定义
执行时已经过了。
根据您的使用情况,处理此问题的常用方法是做
>
def x(lst =无):
如果lst为None:
pass #lst尚未设置为任何内容
:其他:
传递#lst已设置为某物
http://docs.python.org/ref/function.html
"Default parameter values are evaluated when the function definition
is executed."
Depending on your use the common way to handle this is to do
def x(lst = None):
if lst is None:
pass # lst has not been set to anything
else:
pass # lst has been set to something
2008年8月22日星期五11:13:52 + 0200,Bart van Deenen写道:
On Fri, 22 Aug 2008 11:13:52 +0200, Bart van Deenen wrote:
我偶然发现了一条我根本不懂的蟒蛇行为。
I''ve stumbled onto a python behavior that I don''t understand at all.
....
....
是否有人指向描述此行为的语言文档?
Does anyone have any pointers to the language documentation where this behavior is described?
是的,它在FAQ中记录:
http://www.python.org/doc/faq/general/
问题4.22。
-
问候,
Wojtek Walczak,
http://tosh.pl/gminick/
嗨
谢谢大家的回答。我已经找到了你的解决方案,但现在我明白了行为的来源。还有一个问题:我可以在某个地方找到我的参数吗?我看了很多东西,却找不到它。
谢谢
巴特。
> co*********@gmail.com 写道:
Hi
Thanks all for your answers. I figured your solution already, but now I understand where the behavior is from. One question remains: can I find my parameter ''l'' somewhere? I looked in a lot of objects, but couldn''t find it.
Thanks
Bart.
co*********@gmail.com wrote:
8月22日上午11:13,Bart van Deenen
< b ... @ at.vandeenensupport.punt.com.invalidwrote:
On Aug 22, 11:13 am, Bart van Deenen
<b...@at.vandeenensupport.punt.com.invalidwrote:
>>
#function
def X(l = []):
l.append(1)
打印l
#第一次打电话给X
[1]
第二次打电话给X
X()
[1,1]
列表参数''l'在X()的两个连续调用之间存在。为什么不用空列表重新创建它?
这是正确的行为还是Python错误?
是否有人指向描述此行为的语言文档?
>>
# function
def X(l=[]):
l.append(1)
print l
# first call of X
X()
[1]
#second call of X
X()
[1, 1]
Where does the list parameter ''l'' live between the two successive calls
of X(). Why is it not recreated with an empty list?
Is this correct behavior or is it a Python bug?
Does anyone have any pointers to the language documentation where this
behavior is described?
"当执行函数定义
时,将评估默认参数值。
根据您的使用,处理此问题的常用方法是使用
def x(lst =无):
如果没有:
传递#lst没有被设置为任何东西
else:
传递#lst已被设置为
"Default parameter values are evaluated when the function definition
is executed."
Depending on your use the common way to handle this is to do
def x(lst = None):
if lst is None:
pass # lst has not been set to anything
else:
pass # lst has been set to something
这篇关于def X(l = []):怪异。 Python bug?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!