Is 'everything' a refrence or isn't it?
Claudio Grondi
claudio.grondi at freenet.de
Thu Jan 5 04:57:51 EST 2006
Stuart D. Gathman wrote:
> On Wed, 04 Jan 2006 10:54:17 -0800, KraftDiner wrote:
>
>
>>I was under the assumption that everything in python was a refrence...
>>
>>so if I code this:
>>lst = [1,2,3]
>>for i in lst:
>> if i==2:
>> i = 4
>>print lst
>>
>>I though the contents of lst would be modified.. (After reading that
>>'everything' is a refrence.)
>>...
>>Have I misunderstood something?
>
>
> It might help to do a translation to equivalent C:
>
> int _i1 = 1;
> int _i2 = 2;
> int _i3 = 3;
> int _i4 = 4;
> int* lst[NLST] = { &_i1,&_i2,&_i3 };
> int _idx; /* internal iterator */
> for (_idx = 0; _idx < NLST; ++_idx) {
> int *i = lst[_idx];
> if (*i == *_i2)
^-- I have trouble with this line. Is it as is should be? I suppose
it is not.
Claudio
> i = &_i4;
> }
> for (_idx = 0; _idx < NLST; ++_idx)
> printf("%d\n",*lst[_idx]);
>
More information about the Python-list
mailing list