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

Grant Edwards grante at visi.com
Wed Jan 4 14:17:13 EST 2006


On 2006-01-04, KraftDiner <bobrien18 at yahoo.com> wrote:

> I was under the assumption that everything in python was a refrence...

It is.

> 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..

Nope.  "i = 4" doesn't modify the object.  It changes "i" to
point to a different object (one that is an integer value 4).

> (After reading that
> 'everything' is a refrence.)
> so it seems that in order to do this I need to code it like:
>
> lst = [1,2,3]
> for i in range(len(lst)):
>    if lst[i] == 2:
>       lst[i]=4
> print lst
>
> Have I misunderstood something?

Sort of.

You've misunderstood what the statement "i = 4" means.

-- 
Grant Edwards                   grante             Yow!  I'LL get it!! It's
                                  at               probably a FEW of my
                               visi.com            ITALIAN GIRL-FRIENDS!!



More information about the Python-list mailing list