Is 'everything' a refrence or isn't it?

Stuart D. Gathman stuart at bmsi.com
Wed Jan 4 22:38:06 EST 2006


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 = &_i4;
}
for (_idx = 0; _idx < NLST; ++_idx)
  printf("%d\n",*lst[_idx]);

-- 
	      Stuart D. Gathman <stuart at bmsi.com>
Business Management Systems Inc.  Phone: 703 591-0911 Fax: 703 591-6154
"Confutatis maledictis, flamis acribus addictis" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.




More information about the Python-list mailing list