returning a list: IndexError

Steven Bethard steven.bethard at gmail.com
Thu Mar 31 19:20:00 CEST 2005


shama.bell at gmail.com wrote:
> Thats right. I wanted c1 and c2 to retrieve the values returned by t1
> and t2 . Values for t1 and t2 could be anything. Also tbl is global.

Then you need to return t1 and t2 in test, e.g.:

py> import numarray as na
py> tbl = na.zeros((32, 16))
py> def test():
...     t1 = 0x0
...     t2 = 0x1
...     return t1, t2, tbl[t1, t2]
...
py> def get_value():
...     c1, c2, data = test()
...     print tbl[c1, c2]
...
py> get_value()
0

Just as an element of any other container doesn't know what index it's 
stored at, values in a numarray array don't either.  If you want to deal 
with indices, you need to either pass them around instead (or in 
addition to) the values, or you need to search the whole array each time 
for similar objects and see what indices they're at.  The first approach 
is probably preferred in most cases.

STeVe



More information about the Python-list mailing list