[Tutor] Write array to Status text

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Nov 17 21:31:10 EST 2003



On Mon, 17 Nov 2003, Stanfield, Vicki {D167~Indianapolis} wrote:

> I don't know if this made it to the list or not. Here is the code that I
> tried. It doesn't work correctly. The new values don't get appended to
> the array.

Hi Vicki,


Sorry for abruptly jumping in, but I'm getting the feeling we shouldn't be
using the 'array' module here.


A Python 'List' is a collection that can dynamically grow in size, and
that's probably what you want to use for general use.  We can construct
lists by using '[]'.


An 'array' is meant to be a statically-sized homogenous collection of
primitive objects --- like numbers --- and it has a very very specialized
purpose that most folks won't need to touch.  I guess I'm trying to say
politely: don't use them now!  *grin*



> ------------------------------------
> errorarray=array('i', [0]*10)
>
> <snip part where returnedval gets set to hexadecimal value.>
>
> if count == 1:
>          errorarray.append(returnedval)
>
> StatusText=errorarray[0:4].tostring()
> self.frame.SetStatusText(StatusText)
> print errorarray[0:4]
> ------------------------------------


Hmmm...  How about this?


###
errorarray=[]

if count == 1:
    errorarray.append(returnedval)

StatusText = str(errorarray[0:4])
self.frame.SetStatusText(StatusText)
###


SetStatusText() probably expects a string.  The subexpression above:

    errorarray[0:4]

is a slice of the list 'errorarray', but it's still a list --- most API's
that expect a string will NOT automagically convert objecst to strings.


Since string conversion isn't an automatic thing, we need to explicitely
wrap the value with str():

    str(errorarray[0:4])

so that it produces a string representation of the list for
SetStatusText().



If you have questions on any of this, please feel free to ask.  Good luck!




More information about the Tutor mailing list