Class data being zapped by method

Kevin M arenium at gmail.com
Tue Aug 8 18:42:51 EDT 2006


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:
> arenium at 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




More information about the Python-list mailing list