[Tutor] Looping over lists of objects

Etrade Griffiths etrade.griffiths at dsl.pipex.com
Mon Apr 24 15:26:40 CEST 2006


Hi

just feeling my way into Python with a  small app that reads data from 
file, creates objects using that data, stores the objects in a list, loops 
over the list doing comparison tests to filter out various objects.  Here 
is a code snippet:

class myObj:
     def __init__(self,a,b):
         self.a=a
         self.b=b

     def get_a(self):
         return self.a

     def get_b(self):
         return self.b


# Read data from file

L1=[]
nobj=0

for line in input:
         L0=line.split()
         a=L0[1]
         b=L0[2]
         nobj=nobj+1

         an_obj=myObj(a,b)

         L1.append(an_obj)

# Filter data

for i in range(1,nobj):
         for x in L1:            # ... loop over all objects in list
                 a=x.get_a()             # ... get value of a from current 
object
                 b=x.get_b()

                 if test(a,b):
                 print 'test succeeded'
                 else:
                 print 'test failed'

Trying to debug this using IDLE.  The calls x.get_a and x.get_b always 
return zero so something is going wrong somewhere.  I think I'm either not 
storing the objects correctly or retrieving them correctly but no idea 
why!  All suggestions gratefully received!!!

Thanks in advance

Alun Griffiths 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060424/491f4d31/attachment.html 


More information about the Tutor mailing list