Class data being zapped by method

Gary Herron gherron at islandtraining.com
Tue Aug 8 15:50:28 EDT 2006


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