Objects in Python

Ian Kelly ian.g.kelly at gmail.com
Wed Aug 22 13:29:18 EDT 2012


In addition to the excellent feedback that Dave gave you:

On Wed, Aug 22, 2012 at 9:25 AM, shaun <shaun.wiseman91 at gmail.com> wrote:
>         def breakuparray(self):
>                 for row in self.array:
>                         mer = row[0].ljust(25, ' ')
>                         merc = row[1].ljust(13, ' ')
>                         mertype = row[2]
>                         merloc = row[3]
>                         mercount = row[4]
>                         mersec = row[5]
>                         acq = row[6]

This loops over each row in self.array and stores the contents in a
set of variables called mer, merc, etc.  Note that as written these
variables are *local* to the breakuparray method, not attributes of
the class.  Also note that on each iteration, you reassign to the same
variables again, wiping out all the work you did on the previous
iteration.  In the end, only the last row is stored in the local
variables, and then even that is wiped out when the method returns.

>         def returnBatch(self):
>                 self.breakuparray()
>                 return "\x01001\x0251.%s%s%s%s%s%s%s%s\x03"  % (mer, merc, mertype, merloc, mercount, mersec, acq);

This appears to be trying to use the same local variables from the
breakuparray method, but it can't.  Those are out of scope here.  If
you want to share these data between the breakuparray method and the
returnBatch method, you should either have breakuparray return them,
or you should store them as attributes on the object instance:
self.mer, self.merc, self.mertype, etc. (In Python, object attribute
storage is always explicit, never implicit as in Java and the rest of
the C++ family).



More information about the Python-list mailing list