0

I have a relatively small python program, which (partly) looks something like the following:

puredata=ClassOne()
stuff=ClassTwo()

for i in range(0,len(chunks)):
    print "this is in loop  i  = %d" % i
    temp=chunks[i]
    #do some stuff and get variable data.

    for k in range(0,len(data)):
        print "this is in loop  k  = %d" % k
        if puredata.tb in str(temp[k])!=False:
            print "This is valid data..continue calculations"
            puredata.pl,puredata.pt,puredata.pi=stuff.extracts(data[k])
            print "Length of pl puredata = %d" % len(puredata.pl)
            print "Length of pt puredata = %d" % len(puredata.pt)
            print "Length of pi puredata = %d" % len(puredata.pi)
        else:
            pass

The stuff.extracts() spits out 3 lists (something like self.a, self.b, self.c), which I then pass on to puredata.pl,puredata.pt,puredata.pi and ClassOne simply invokes the lists pl,pt,pi. ClassTwo looks something along the lines of:

class ClassTwo():
    def __init__(self):
      self.a=[]
      self.b=[]
      self.c=[]
# Start calculations
    def extracts(self,soup):
        # do stat modelling and calculations
          return self.a, self.b,self.c

When I run the loop, I find that data seems to "appended" to puredata.pl,puredata.pt,puredata.pi although I have never used the .append() anywhere in the code. So, in the first loop the length of puredata.pl,puredata.pt,puredata.pi is say 10, and in the next loop it becomes 20, then 30..and so on..

I am finding this very strange - and I am not able to make sense out of it. At some level, this is what I want (I mean the appending of data to a list), but I am baffled why it appends although I have not used .append().

Sorry if this is a kn00b question - python newbie here.

7
  • if puredata.tb in str(temp[k])!=False: is parsed as (I think): if (puredata.tb in str(temp[k]))!=False: which makes it more or less equivalent to if puredata.tb in str(temp[k]): Commented Nov 15, 2012 at 15:28
  • @mgilson, yea I see what you mean - but I do not think that this is the reason I have an appended list without me asking it to append - do you think it is? Commented Nov 15, 2012 at 15:31
  • definitely not the problem. It if was the problem, I would have posted that as an answer :). It's just something that I found confusing while I was reading over the code and I thought I'd let you know that was a very strange statement. Commented Nov 15, 2012 at 15:35
  • @mgilson thats fine - I have changed that part of the code now. Still unsure why it would append all results to my list! Commented Nov 15, 2012 at 15:37
  • How are the lists returned from .extracts? Are they references or copies? Commented Nov 15, 2012 at 15:38

1 Answer 1

2

The mostly likely problem, based on what you have provided, is that the stuff.a stuff.b and stuff.c are growing each time you call stuff.extracts. If the stat modelling and calculations being performed in ClassTwo are unique to each data[k], then declaring stuff = ClassTwo() inside of the loop should resolve your problem.

e.g:

for k in range(0,len(data)):
    stuff = ClassTwo()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for that tip - what you say seem to make sense... thats the stuff.a,stuff.b and stuff.c seem to be growing every time. I will check this now.
You were correct - that is exactly what was happening. Putting the class inside the loop solved the problem! thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.