global lists
Bernd Nawothnig
Bernd.Nawothnig at t-online.de
Sun May 8 07:20:17 EDT 2005
On 2005-05-08, andrea crotti wrote:
> I have a little "problem", I don't understand the reason of this:
> a = [10,1,2,3]
> def foo():
> global a
> for el in a:
> el = el*2
Simple data types (as integer) are _not_ implemented as references as
you obviously expected. Instead el is copied by value from each a[n].
Thus you only changed temporary copies.
The whole list instead _is_ a reference. If you write
b = a
b[0] = 57
a[0] will be effected too.
> This doesn't make any difference, if I do
> def foo():
> global a
> a[0] = 4
That should alter a[0].
> But
>
> def foo():
> global a
> for n in range(len(a)):
> a[n] = a[n]*2
>
> Doesn't work either...
It does. Test it again.
Bernd
--
Those who desire to give up freedom in order to gain security,
will not have, nor do they deserve, either one. [T. Jefferson]
More information about the Python-list
mailing list