类数据被方法破坏 [英] Class data being zapped by method

查看:43
本文介绍了类数据被方法破坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我会切入追逐。


我有一个名为Foo()的班级。我创建了一个名为

bar的类的实例,并将bar.data设置为一个大的元组列表。


在Foo()中有一个在self.data上运行的方法。在我从外部设置self.data后,我需要

调用此方法。 (bar.data),

这不是问题。但是,我已经通过简单的调试发现了

程序,虽然在所述方法被调用之前bar.data存在,但是类方法中的self.data是EMPTY。在我的班级

构造函数中,我将self.data声明为空列表([]),但

不应该是self.data包含填充列表吗? br />

基本上......


----------------------- -------------------------------------------------- -

class Foo():

__init __(个体经营):

self.data = []

a_count (个体经营):

....

打印self.data

....


bar = Foo()

bar.data = [(-74.0015,1),(123.451,18),...]

print bar.data#得到我的期望

bar.a_count()#[]

---------------------- -------------------------------------------------- -


这是我正在做的*高度*简化版本。如果

需要更多代码,请询问。


提前致谢。

解决方案

ar*****@gmail.com 写道:





我会切入追逐。


我有一个名为Foo的班级( )。我创建了一个名为

bar的类的实例,并将bar.data设置为一个大的元组列表。


在Foo()中有一个在self.data上运行的方法。在我从外部设置self.data后,我需要

调用此方法。 (bar.data),

这不是问题。但是,我已经通过简单的调试发现了

程序,虽然在所述方法被调用之前bar.data存在,但是类方法中的self.data是EMPTY。在我的班级

构造函数中,我将self.data声明为空列表([]),但

不应该是self.data包含填充列表吗? br />

基本上......


----------------------- -------------------------------------------------- -

class Foo():

__init __(个体经营):

self.data = []

a_count (个体经营):

....

打印self.data

....


bar = Foo()

bar.data = [(-74.0015,1),(123.451,18),...]

print bar.data#得到我的期望

bar.a_count()#[]

---------------------- -------------------------------------------------- -



嗯,......你还没有在这里告诉我们整个故事。此代码用作

预期。


>> class Foo():



.... def __init __(self):

... .self.data = []

.... def a_count(个体经营):

....打印self.data

。 ...


>> bar = Foo()
栏。 data = [(-74.0015,1),(123.451,18)]
print bar.data



[( -74.001499999999993,1),(123.45099999999999,18)]


>> bar。 a_count()



[(-74.001499999999993,1),(123.45099999999999,18)]


>>>



您努力将实际代码减少到简单演示

问题,我们不胜感激,但是在这种情况下,我认为它不起作用。


想再试一次?

加里赫伦


图。我会试着让它变得充分复杂;)

我会试着总结一下发生了什么,但我意识到我是b / b
重新输入什么我已经编程以努力更好地说明我正在做什么。它是Pastebin。不要害怕,它只有大约200行才能获得



类文件 - http://pastebin.4programmers.net/640

主文件 - http://pastebin.4programmers.net/639


请不要担心任何文件解析的东西。这一切都在工作

。问题是,当我们从analyze()调用它时,我在我的

_a_count和_b_count方法中得到了IndexError异常。我为
添加了一些代码以增加冗长度,并且它转变为Test类中的计数

方法在EMPTY列表([])上运行,所以

自然他们不能索引任何东西。这是问题的核心,

特别是考虑到main.py中的第98行按预期工作。


再一次,非常感谢你们解决这种情况,

每个人



Gary Herron写道:

ar ***** @ gmail.com 写道:





我会切入追逐。


我有一个名为Foo()的类。我创建了一个名为

bar的类的实例,并将bar.data设置为一个大的元组列表。


在Foo()中有一个在self.data上运行的方法。在我从外部设置self.data后,我需要

调用此方法。 (bar.data),

这不是问题。但是,我已经通过简单的调试发现了

程序,虽然在所述方法被调用之前bar.data存在,但是类方法中的self.data是EMPTY。在我的班级

构造函数中,我将self.data声明为空列表([]),但

不应该是self.data包含填充列表吗? br />

基本上......


----------------------- -------------------------------------------------- -

class Foo():

__init __(个体经营):

self.data = []

a_count (个体经营):

....

打印self.data

....


bar = Foo()

bar.data = [(-74.0015,1),(123.451,18),...]

print bar.data#得到我的期望

bar.a_count()#[]

---------------------- -------------------------------------------------- -



嗯,...你还没有在这里告诉我们整个故事。此代码可用作预期的



>> class Foo():



... def __init __(self):

... self.data = []

... def a_count(self):

... print self.data

...


>> bar = Foo()

>> bar.data = [(-74.0015,1),(123.451,18)]

>>打印bar.data



[(-74.001499999999993,1),(123.45099999999999,18)]


>> bar.a_count()



[(-74.001499999999993,1),(123.45099999999999,18)]


>>>




您努力将实际代码减少到简单演示

问题,我们不胜感激,但是在这种情况下,我认为它不起作用。


想再试一次?


加里赫伦


Kevin M写道:


数字。我会试着让它充分复杂化;)


我会试着总结一下发生了什么,但我意识到我

正在重新输入我已编程的内容,以便更好地说明
说明我正在做什么。它是Pastebin。不要害怕,它只有大约200行才能获得



类文件 - http://pastebin.4programmers.net/640



任何*好*原因你有(a)以任意的方式分割你的解决方案[某些课程,有些不是](b)将课程分成一个单独的

文件?


主文件 - http:// pastebin.4programmers.net/639


请不要担心任何文件解析的东西。这一切都在工作

。问题是,当我们从analyze()调用它时,我在我的

_a_count和_b_count方法中得到了IndexError异常。我为
添加了一些代码以增加冗长度,并且它转变为Test类中的计数

方法在EMPTY列表([])上运行,所以

自然他们不能索引任何东西。这是问题的核心,

特别是考虑到main.py中的第98行按预期工作。



第98行是#

print arrTests [2] .data


这并不表示* all * case都有非空列表。

数据附加*有条件*约92行左右。


< side>

try / except增加了另一个级别(完全没必要,AFAICT)

复杂性&填充性 - 你应该以其他方式实现它

左右:


for arrLines:

row = line。 split()[offset:]

为索引,测试为枚举(arrTests):

blah = row [index]

如果不是等等在bad_vals中:



和*不要*忽略IndexError(有了这个重新排列,任何IndexError

都是一个bug)。

< / aside>


为了证明你有一个真正的问题:


(1)替换行98 by:

print [i,len(x.data),id(x)for i,x in enumerate(arrTests)]


(2 )在你的类文件中的第26行之后,插入


print id(self),len(self.data)


[id( )将明确地告诉你哪个测试对象有问题

当/如果_a_sort方法失败时]


HTH,

John


PS下次,显示您的实际输出。


Hi,

I''ll cut to the chase.

I have a class named Foo(). I create an instance of this class named
bar, and I set bar.data to a large list of tuples.

Within Foo() there is a method which operates on self.data. I need to
call this method after I set self.data from the "outside" (bar.data),
which isn''t a problem. However, I have found through simple debugging
procedures that while bar.data exists fine before the said method is
called, self.data within the class method is EMPTY. In my class
constructor I do declare self.data to be an empty list ([]), but
shouldn''t self.data contain the populated list?

Basically...

--------------------------------------------------------------------------
class Foo():
__init__(self):
self.data = []
a_count(self):
....
print self.data
....

bar = Foo()
bar.data = [(-74.0015, 1), (123.451, 18), ...]
print bar.data # Get what I expect
bar.a_count() # []
--------------------------------------------------------------------------

This is a *highly* simplified version of what I''m actually doing. If
more code is needed just ask.

Thanks in advance.

解决方案

ar*****@gmail.com wrote:

Hi,

I''ll cut to the chase.

I have a class named Foo(). I create an instance of this class named
bar, and I set bar.data to a large list of tuples.

Within Foo() there is a method which operates on self.data. I need to
call this method after I set self.data from the "outside" (bar.data),
which isn''t a problem. However, I have found through simple debugging
procedures that while bar.data exists fine before the said method is
called, self.data within the class method is EMPTY. In my class
constructor I do declare self.data to be an empty list ([]), but
shouldn''t self.data contain the populated list?

Basically...

--------------------------------------------------------------------------
class Foo():
__init__(self):
self.data = []
a_count(self):
....
print self.data
....

bar = Foo()
bar.data = [(-74.0015, 1), (123.451, 18), ...]
print bar.data # Get what I expect
bar.a_count() # []
--------------------------------------------------------------------------

Well, ... you have not told us the whole story here. This code works as
expected.

>>class Foo():

.... def __init__(self):
.... self.data = []
.... def a_count(self):
.... print self.data
....

>>bar = Foo()
bar.data = [(-74.0015, 1), (123.451, 18)]
print bar.data

[(-74.001499999999993, 1), (123.45099999999999, 18)]

>>bar.a_count()

[(-74.001499999999993, 1), (123.45099999999999, 18)]

>>>


You effort to reduce your real code to a simple demonstration of the
problem is appreciated, but I don''t think it worked in this case.

Want to try again?
Gary Herron


Figures. I''ll try to complicate it sufficiently ;)
[edit] I was going to try to sum up what goes on, but I realized that I
was retyping what I already programmed in an effort to better
illustrate exactly what I''m doing. Pastebin it is. Don''t fear, it''s
only around 200 lines total.

Class file -- http://pastebin.4programmers.net/640
Main file -- http://pastebin.4programmers.net/639

PLEASE don''t worry about any file parsing stuff. That''s all working
well. The problem is that I''m getting IndexError exceptions in my
_a_count and _b_count methods when they''re called from analyze(). I
added some code to increase verbosity, and it turns that the count
methods within the Test class are operating on EMPTY lists ( [] ), so
naturally they can''t index anything. This is the core of the problem,
especially considering that line 98 in main.py works as expected.

Once again, thank you VERY VERY much for addressing this situation,
everyone


Gary Herron wrote:

ar*****@gmail.com wrote:

Hi,

I''ll cut to the chase.

I have a class named Foo(). I create an instance of this class named
bar, and I set bar.data to a large list of tuples.

Within Foo() there is a method which operates on self.data. I need to
call this method after I set self.data from the "outside" (bar.data),
which isn''t a problem. However, I have found through simple debugging
procedures that while bar.data exists fine before the said method is
called, self.data within the class method is EMPTY. In my class
constructor I do declare self.data to be an empty list ([]), but
shouldn''t self.data contain the populated list?

Basically...

--------------------------------------------------------------------------
class Foo():
__init__(self):
self.data = []
a_count(self):
....
print self.data
....

bar = Foo()
bar.data = [(-74.0015, 1), (123.451, 18), ...]
print bar.data # Get what I expect
bar.a_count() # []
--------------------------------------------------------------------------

Well, ... you have not told us the whole story here. This code works as
expected.

>>class Foo():

... def __init__(self):
... self.data = []
... def a_count(self):
... print self.data
...

>>bar = Foo()
>>bar.data = [(-74.0015, 1), (123.451, 18)]
>>print bar.data

[(-74.001499999999993, 1), (123.45099999999999, 18)]

>>bar.a_count()

[(-74.001499999999993, 1), (123.45099999999999, 18)]

>>>



You effort to reduce your real code to a simple demonstration of the
problem is appreciated, but I don''t think it worked in this case.

Want to try again?
Gary Herron


Kevin M wrote:

Figures. I''ll try to complicate it sufficiently ;)
[edit] I was going to try to sum up what goes on, but I realized that I
was retyping what I already programmed in an effort to better
illustrate exactly what I''m doing. Pastebin it is. Don''t fear, it''s
only around 200 lines total.

Class file -- http://pastebin.4programmers.net/640

Any *good* reason you have (a) split your solution in an arbitrary
fashion [some in a class, some not] (b) put the class in a separate
file?

Main file -- http://pastebin.4programmers.net/639

PLEASE don''t worry about any file parsing stuff. That''s all working
well. The problem is that I''m getting IndexError exceptions in my
_a_count and _b_count methods when they''re called from analyze(). I
added some code to increase verbosity, and it turns that the count
methods within the Test class are operating on EMPTY lists ( [] ), so
naturally they can''t index anything. This is the core of the problem,
especially considering that line 98 in main.py works as expected.

Line 98 is #
print arrTests[2].data

This doesn''t show that *all* cases have non-empty lists.
Data is appended *conditionally* around about line 92.

<aside>
The try/except adds another level of (totally unnecessary, AFAICT)
complexity & stuffupability -- you should really do it the other way
around:

for line in arrLines:
row = line.split()[offset:]
for index, test in enumerate(arrTests):
blah = row[index]
if blah not in bad_vals:
etc
and *don''t* ignore IndexError (with this rearrangement, any IndexError
is a bug).
</aside>

To demonstrate that you have a real problem:

(1) replace line 98 by:
print [i, len(x.data), id(x) for i, x in enumerate(arrTests)]

(2) after line 26 in your class file, insert

print id(self), len(self.data)

[The id() will tell you unequivocably which test object has the problem
when/if the _a_sort method fails]

HTH,
John

P.S. Next time, show your actual output.


这篇关于类数据被方法破坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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