a small problem
Justin Sheehy
dworkin at ccs.neu.edu
Tue Jan 11 20:40:46 EST 2000
knotwell at my-deja.com writes:
> Is the following a bug?
>
> >>> q = [(1,'dd'),(2,'ddd'),(3,'ddddd')]
>
> >>> reduce(lambda x,y: x[0] + y[0],q)
>
> Traceback (innermost last):
> File "<stdin>", line 1, in ?
> File "<stdin>", line 1, in <lambda>
> AttributeError: __getitem__
If you expand the reduce call, the problem becomes obvious:
((1,'dd')[0] + (2,'ddd')[0])[0] + (3,'ddddd')[0]
In the first step, you do (1,'dd')[0] + (2,'ddd')[0], which is fine.
It evaluates to the integer 3.
The next step, you do 3[0] + (3,'ddddd')[0], which will fail because
the result of the previous step (3) cannot be subscripted.
-Justin
More information about the Python-list
mailing list