Bug or feature?

Terry Reedy tjreedy at udel.edu
Fri Jan 16 21:24:46 CET 2004


"Dennis Lee Bieber" <wlfraed at ix.netcom.com> wrote in message
news:3bhld1-914.ln1 at beastie.ix.netcom.com...
>         But, since Python is dynamically typed, reversing the evaluation
order
> could have major effects on other operations... The "problem" is not
> the order of evaluation, but that you have a "hidden" modification to a
> term occurring.
>
> >>> def se(a):                  #side-effect
> ...     a.insert(0,3)
> ...     return a
> ...
> >>> def nose(a):                #no side-effect
> ...     a = [3] + list(a)
> ...     return a
> ...
> >>> l = [1,5,7]
> >>> l += nose(l)
> >>> l
> [1, 5, 7, 3, 1, 5, 7]
> >>> l = [1,5,7]
> >>> l += se(l)
> >>> l
> [3, 1, 5, 7, 3, 1, 5, 7]

Lovely example, no hypotheticals needed.

For those who did not get it, the key is that Python expressions evaluate
to objects, and that the + operator does not reach into the objects to pull
out values for the new object until it has (references to) both objects,
which in the second case, are both the same.

Terry J. Reedy





More information about the Python-list mailing list