__getslice__

Hans Nowak ivnowa at hvision.nl
Mon Nov 29 15:37:35 EST 1999


On 29 Nov 99, Stuart Reynolds wrote:

> >>> def square(x):
>         return x*x
> 
> >>> xvals = [0,1,2,3,4,5,6,7]
> >>> a = UnaryFnSequence(square, xvals)
> >>> xvals[1:]
> [1, 2, 3, 4, 5, 6, 7]
> >>> a
> ( (0,0), (1,1), (2,4), (3,9), (4,16), (5,25), (6,36), (7,49) )
> >>> a[1:]
> ( (0,0), (1,1), (2,4), (3,9), (4,16), (5,25), (6,36) )
> 
> Note that the first item is still there. The last one's gone instead.

Did you try UnaryFnSequence(square, [2,3,4])? You'll get a surprising 
result:

((0,0), (1,1), (2,4))

>     def __repr__(self):
>         s='( '
>         for x in range( len(self) ):
>             s = s+'('+`x`+','+`self._fn(x)`+'), '
>         return s[:-2] + ' )'

This is the culprit... You take a list of N elements, and then you 
loop over *a range 0..N-1*... rather than the list! Try replacing the

  for x in range(len(self)):

with

  for x in self._inputs:

This might do what you intended. ^_^

Hope this helps,

--Hans Nowak (zephyrfalcon at hvision.nl)
Homepage: http://fly.to/zephyrfalcon
You call me a masterless man. You are wrong. I am my own master.




More information about the Python-list mailing list