__init__中的默认值 [英] default value in __init__

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

问题描述

亲爱的,


我遇到了这个奇怪的问题。


我有一个带有__init__参数''d'的类定义'

,默认为{}。这个参数放在初始化''self.d''

属性中


我创建了这个类的两个独立实例;代码

如下。


class C:

def __init __(self,i = 10,d = {} ):

self.d = d

self.i = i

def get(self):

打印

打印self.d

def set(self,dval,ival):

self.d.update(dval)

self.i + = ival


c1 = C()

c1.set({''one'':1},3 )

c1.get()


del c1


c2 = C()

c2.set({''two'':2},4)

c2.get()

如果我运行我获得的代码:


{''one'':1}


{''two'':2,''one'':1}


似乎第二个实例的''self.d''参数是第一个''self.d''的

'(实例。


在调试器中运行代码我发现,当我在第二次初始化时输入

__init__,然后再执行


self.d = d

'''''变量已包含第一个

实例的''self.d''值,而不是默认参数{}。


我做了一些愚蠢的错误,或者这是一个问题吗?


提前感谢您的帮助,

Paolo

Dear all,

I have encountered this weird problem.

I have a class definition with an __init__ argument ''d''
which defaults to {}. This argument is put in the ''self.d''
attribute at initialization

I create two independent instances of this class; the code
is as follows.

class C:
def __init__(self, i=10, d = {}):
self.d = d
self.i = i
def get(self):
print
print self.d
def set(self, dval, ival):
self.d.update(dval)
self.i+=ival

c1=C()
c1.set({''one'':1},3)
c1.get()

del c1

c2=C()
c2.set({''two'':2},4)
c2.get()
If I run the code I obtain:

{''one'': 1}

{''two'': 2, ''one'': 1}

It seems that the ''self.d'' argument of the second instance is the
same of the ''self.d'' of the first (deleted!) instance.

Running the code in a debugger I discovered that, when I enter the
__init__ at the second initialization, before doing

self.d = d

the ''d'' variable already contains the ''self.d'' value of the first
instance and not the default argument {}.

Am I doing some stupid error, or this is a problem ?

Thanks in advance for any help,
Paolo

推荐答案

http://zephyrfalcon.org/labs/python_pitfalls.html

它也适用于字典(和集合,任何可变对象)。


2008年10月9日星期四凌晨1点03分,kenneth< ke ***** @ inwind.itwrote:
See Pitfall #5 on http://zephyrfalcon.org/labs/python_pitfalls.html
It also applies to dictionaries (and sets, any mutable object really).

On Thu, Oct 9, 2008 at 1:03 AM, kenneth <ke*****@inwind.itwrote:

亲爱的所有,


我遇到了这个奇怪的问题。


我有一个带有__init__参数的类定义'''''/ br >
,默认为{}。这个参数放在初始化''self.d''

属性中


我创建了这个类的两个独立实例;代码

如下。


class C:

def __init __(self,i = 10,d = {} ):
Dear all,

I have encountered this weird problem.

I have a class definition with an __init__ argument ''d''
which defaults to {}. This argument is put in the ''self.d''
attribute at initialization

I create two independent instances of this class; the code
is as follows.

class C:
def __init__(self, i=10, d = {}):



将''d = {}''更改为''d =无''

添加以下行:

如果d为无:d = {}


干杯,

克里斯

-

沿着鬣蜥的路径......
http://rebertia.com


self.d = d

self.i = i

def get(self) :

打印

打印self.d

def set(self,dval,ival):

self。 d.update(dval)

self.i + = ival


c1 = C()

c1.set({' 'one'':1},3)

c1.get()


del c1


c2 = C()

c2.set({''two'':2},4)

c2.get()


如果我运行我获得的代码:


{''one'':1}


{''two '':2,''one'':1}


似乎第二个实例的''self.d''参数是

与第一个(已删除!)实例的''self.d'相同。


在调试器中运行代码我发现,当我进入

__init__在第二次初始化时,在做之前


self.d = d


'''''变量已经包含第一个

实例的''self.d''值,而不是默认参数{}。


我做了一些愚蠢的错误,或者这个有问题吗?


提前感谢您的帮助,

Paolo

-
http://mail.python.org/mailman/listinfo/python-list


kenneth写道:
kenneth wrote:

'''''变量已包含'第一个

实例的'self.d''值,而不是默认参数{}。


我做了一些愚蠢的错误,或者这是一个问题?
the ''d'' variable already contains the ''self.d'' value of the first
instance and not the default argument {}.

Am I doing some stupid error, or this is a problem ?



不,它总是包含默认参数,因为默认值是

一次创建。
http://effbot.org/pyfaq/why-are -defa ... en-objects.htm

No, it always contains the default argument because default values are
created just ONE TIME.
http://effbot.org/pyfaq/why-are-defa...en-objects.htm


10月9日上午10:14 *,Christian Heimes< li .. 。@ cheimes.dewrote:
On Oct 9, 10:14*am, Christian Heimes <li...@cheimes.dewrote:

kenneth写道:
kenneth wrote:

''d''变量已包含第一个

实例的''self.d''值,而不是默认参数{}。
the ''d'' variable already contains the ''self.d'' value of the first
instance and not the default argument {}.


我做了一些愚蠢的错误,或者这是一个问题?
Am I doing some stupid error, or this is a problem ?



不,它总是包含默认参数,因为默认值是

一次创建。 http://effbot.org/pyfaq/why-are-defa...etween - 对象 ...


No, it always contains the default argument because default values are
created just ONE TIME.http://effbot.org/pyfaq/why-are-defa...etween-objects...



哇,这是非常危险的行为......


只是要知道,这是写在python文档中的某个地方还是

当他的程序无法工作时必须发现它; - )?


Paolo


Wow, it''s a very "dangerous" behavior ...

Just to know, is this written somewhere in the python documentation or
one has to discover it when his programs fails to work ;-) ?

Paolo


这篇关于__init__中的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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