空列表作为默认参数 [英] Empty list as default parameter

查看:68
本文介绍了空列表作为默认参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


也许我很愚蠢,但我只是不明白为什么以下

代码表现得像它一样:


- = - = - = -


类listHolder:

def __init __(self,myList = []):

self.myList = myList


def __repr __(self):return str(self.myList)


#debug:''''应该包含42,''b''应为空。但没有。

a = listHolder()

a.myList.append(42)

b = listHolder()

打印一份

打印b

- = - = - = -


我期待看到[ 42]然后[],但我看到[42]然后[42]。它似乎a b和b共享对同一列表对象的引用。为什么?


-

<<<< Alexspudros Potatopoulos>>>

Spudkind的后卫

解决方案

Alex Panayotopoulos写道:

大家好,

也许我是愚蠢的,但我只是不明白为什么以下
代码的行为如下:

- = - = - = -

class listHolder:
def __init __(self,myList = []):
self.myList = myList

def __repr __(self):return str(self.myList)

#debug:''''应该包含42,''b''应为空。但没有。
a = listHolder()
a.myList.append(42)
b = listHolder()
打印
打印b

- = - = - = -

我期待看到[42]然后[],但我看到[42]然后[42]。它似乎a和b共享对同一列表对象的引用。为什么?




真正常见的错误:列表是_mutable_对象和self.myList

引用与默认参数相同的对象。因此

a.myList.append也会修改默认值。可以更好地避免可变默认值(除了备忘录模式的某些变体之外)。标准

技巧是:


def __init __(self,myList = None):

如果myList为None:

self.myList = []

else:

self.myList = myList


问候,

anton。


>

class listHolder:
def __init __(self,myList = []):
self.myList = myList

def __repr __(self):return str(self.myList)

#debug:''a' '应该包含42,''b''应该是空的。但没有。
a = listHolder()
a.myList.append(42)
b = listHolder()
打印
打印b

- = - = - = -

我期待看到[42]然后[],但我看到[42]然后[42]。它似乎a和b共享对同一列表对象的引用。为什么?




您好,

AFAIK,默认参数值仅实例化一次。所以,

default-myList始终是同一个对象。上面的编码应该是

改写为...


class listHolder:

def __init __(self,myList = None) :

if myList = None:

self.myList = []

else:

self.myList = myList


问候,

Thorsten



Alex Panayotopoulos写道:

也许我是愚蠢的,但我只是不明白为什么下面的代码表现得像:

- = - = - = -

类listHolder:
def __init __(self,myList = []):
self.myList = myList

def __repr __(self):return str(self.myList)
#debug:''''应该包含42,''b''应该为空。但没有。
a = listHolder()
a.myList.append(42)
b = listHolder()
打印
打印b

- = - = - = -

我期待看到[42]然后[],但我看到[42]然后[42]。它似乎a和b共享对同一列表对象的引用。为什么?




当创建函数

对象时,默认值表达式被计算一次,结果对象绑定到参数。


如果你想在每个电话上创建一个新对象,你必须自己做




def __init __(self,myList = None):
如果myList为None,则为


myList = []#创建新列表

self.myList = myList


或者:


def __init __(self,myList = None):

self.myList = myList或[]


请参阅def的说明。更多信息声明:

http ://www.python.org/doc/current/ref/function.html


< / F>


Hello all,

Maybe I''m being foolish, but I just don''t understand why the following
code behaves as it does:

- = - = - = -

class listHolder:
def __init__( self, myList=[] ):
self.myList = myList

def __repr__( self ): return str( self.myList )

# debug: ''a'' should contain 42, ''b'' should be empty. But no.
a = listHolder()
a.myList.append( 42 )
b = listHolder()
print a
print b

- = - = - = -

I was expecting to see [42] then [], but instead I see [42] then [42]. It
seems that a and b share a reference to the same list object. Why?

--
<<<Alexspudros Potatopoulos>>>
Defender of Spudkind

解决方案

Alex Panayotopoulos wrote:

Hello all,

Maybe I''m being foolish, but I just don''t understand why the following
code behaves as it does:

- = - = - = -

class listHolder:
def __init__( self, myList=[] ):
self.myList = myList

def __repr__( self ): return str( self.myList )

# debug: ''a'' should contain 42, ''b'' should be empty. But no.
a = listHolder()
a.myList.append( 42 )
b = listHolder()
print a
print b

- = - = - = -

I was expecting to see [42] then [], but instead I see [42] then [42]. It
seems that a and b share a reference to the same list object. Why?



Really common mistake: lists are _mutable_ objects and self.myList
references the same object as the default parameter. Therefore
a.myList.append modifies default value as well. Mutable defaults are
better avoided (except for some variants of memo pattern). Standard
trick is:

def __init__(self, myList = None):
if myList is None:
self.myList = []
else:
self.myList = myList

regards,
anton.


>

class listHolder:
def __init__( self, myList=[] ):
self.myList = myList

def __repr__( self ): return str( self.myList )

# debug: ''a'' should contain 42, ''b'' should be empty. But no.
a = listHolder()
a.myList.append( 42 )
b = listHolder()
print a
print b

- = - = - = -

I was expecting to see [42] then [], but instead I see [42] then [42]. It
seems that a and b share a reference to the same list object. Why?



Hi,
AFAIK, the default parameter values are only instantiated once. So, the
default-myList is always the same object. The coding above should be
rewritten like...

class listHolder:
def __init__( self, myList=None ):
if myList = None:
self.myList = []
else:
self.myList = myList

Regards,
Thorsten



Alex Panayotopoulos wrote:

Maybe I''m being foolish, but I just don''t understand why the following
code behaves as it does:

- = - = - = -

class listHolder:
def __init__( self, myList=[] ):
self.myList = myList

def __repr__( self ): return str( self.myList )

# debug: ''a'' should contain 42, ''b'' should be empty. But no.
a = listHolder()
a.myList.append( 42 )
b = listHolder()
print a
print b

- = - = - = -

I was expecting to see [42] then [], but instead I see [42] then [42]. It
seems that a and b share a reference to the same list object. Why?



the default value expression is evaluated once, when the function
object is created, and the resulting object is bound to the argument.

if you want to create a new object on every call, you have to do
that yourself:

def __init__( self, myList=None):
if myList is None:
myList = [] # create a new list
self.myList = myList

or perhaps:

def __init__( self, myList=None):
self.myList = myList or []

see the description of the "def" statement for more info:

http://www.python.org/doc/current/ref/function.html

</F>



这篇关于空列表作为默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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