__init__中的错误? [英] Bug in __init__?

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

问题描述

我今天在我的代码中发现了一个错误,花了一个小时试图找到它

然后最小化测试用例。


一旦我做了,我仍然对行为感到困惑,我无法在文档中找到对此行为的任何引用。


testcase:


A级():

def add(self,el):

self.lst.extend(el)


def __init__(自我,val = []):

打印val

self.lst = val

def test():

x = A()

x.add([" foo1"," foo2"])

b = A()

所以,我期望的是,我将创建两个具有空self.lst属性的
A类实例。对吧?


其实(至少用我的Python 2.5)


gandalf @ gandalf-desktop:〜/ projects / pyl10n $ ./ scripts / test.py

[]

[''foo1'',''foo2'']


这个bug当我切换到__init__(自我,* args)和

分配self.lst = args [0]时不会发生。


任何关于什么的线索在这里,和/如果我应该报告它?


问候

Zbigniew Braniecki

解决方案

./ scripts / test.py

[]

[''foo1'',''foo2'']


当我切换到__init__(self,* args)和

分配self.lst = args [0]时,不会发生这个错误。


任何关于这里发生了什么的线索,以及/我应该在哪里报告?


问候

Zbigniew Braniecki


Zbigniew Braniecki写道:


任何c请问这里发生了什么,以及/我应该在哪里举报?



恭喜!你已经偶然发现了一个众所周知的陷阱。大多数新手

陷入陷阱。


A级:

def __init__(自我,val = []):

打印val

self.lst = val


val仅创建*一次*并在A的所有实例中共享。


Christian


2008年1月18日下午3:09,Zbigniew Braniecki

< zb **************** @ gmail.comwrote:


我今天在我的代码中发现了一个错误,并花了一个小时试图找到它

然后最小化测试用例。


一旦我做了,我仍然对行为感到困惑,我不能

在docs中找到对这种行为的任何引用。


testcase:


class A():


def add(self,el):

self.lst.extend(el)


def __init__ (self,val = []):

打印val

self.lst = val


def test():

x = A()

x.add([" foo1"," foo2"])

b = A()


所以,我在这里期望的是,我将创建两个具有空self.lst属性的
A类实例。对吗?


其实(至少用我的Python 2.5)


gandalf @ gandalf-desktop:〜/ projects / pyl10n

I found a bug in my code today, and spent an hour trying to locate it
and then minimize the testcase.

Once I did it, I''m still confused about the behavior and I could not
find any reference to this behavior in docs.

testcase:

class A():

def add (self, el):
self.lst.extend(el)

def __init__ (self, val=[]):
print val
self.lst = val
def test ():
x = A()
x.add(["foo1","foo2"])
b = A()
So, what I would expect here is that I will create two instances of
class A with empty self.lst property. Right?

In fact (at least with my Python 2.5)

gandalf@gandalf-desktop:~/projects/pyl10n$ ./scripts/test.py
[]
[''foo1'', ''foo2'']

This bug does not happen when I switch to __init__ (self, *args) and
assign self.lst= args[0].

Any clue on what''s going on here, and/if where I should report it?

Greetings
Zbigniew Braniecki

解决方案

./scripts/test.py
[]
[''foo1'', ''foo2'']

This bug does not happen when I switch to __init__ (self, *args) and
assign self.lst= args[0].

Any clue on what''s going on here, and/if where I should report it?

Greetings
Zbigniew Braniecki


Zbigniew Braniecki wrote:

Any clue on what''s going on here, and/if where I should report it?

Congratulations! You''ve stumbled over a well known gotcha. Most newbies
fall for the trap.

class A:
def __init__ (self, val=[]):
print val
self.lst = val

val is created only *once* and shared across all instaces of A.

Christian


On Jan 18, 2008 3:09 PM, Zbigniew Braniecki
<zb****************@gmail.comwrote:

I found a bug in my code today, and spent an hour trying to locate it
and then minimize the testcase.

Once I did it, I''m still confused about the behavior and I could not
find any reference to this behavior in docs.

testcase:

class A():

def add (self, el):
self.lst.extend(el)

def __init__ (self, val=[]):
print val
self.lst = val
def test ():
x = A()
x.add(["foo1","foo2"])
b = A()
So, what I would expect here is that I will create two instances of
class A with empty self.lst property. Right?

In fact (at least with my Python 2.5)

gandalf@gandalf-desktop:~/projects/pyl10n


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

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