Simple object reference

Chris Rebert clp2 at rebertia.com
Sun Nov 15 00:40:05 CET 2009


On Sat, Nov 14, 2009 at 3:25 PM, AON LAZIO <aonlazio at gmail.com> wrote:
> Hi, I have some problem with object reference
> Say I have this code
>
> a = b = c = None
> slist = [a,b,c]

Values are stored in the list, not references to names. Modifying the
list does not change what values the names a, b, and c have. There is
no Python-level notion of pointers.

> for i in range(len(slist)):
> slist[i] = 5

This modifies the contents of the list, it does not affect any other
variables/names.

> print slist
> print a,b,c
>
> I got this
> [5, 5, 5]
> None None None
>
>     Question is how can I got all a,b,c variable to have value 5 also?

Use tuple unpacking:

#at start of code
slist = [None]*3
#...
#at end of code
a, b, c = slist

I would also recommend reading
http://effbot.org/zone/call-by-object.htm for a good explanation of
how Python's variables work.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list