Simple object reference
ben+python at benfinney.id.au
Sun Nov 15 01:36:25 CET 2009
Chris Rebert <clp2 at rebertia.com> writes:
> 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.
There *is*, though, a notion of references, which is what the OP is
asking about. The list does not store values, it stores references to
values (just as a name references a value).
You're right that the names are irrelevant for the items in the list;
the name ‘a’ and the item ‘slist’ are always independent references.
> > for i in range(len(slist)):
> > slist[i] = 5
Hence, this has no effect on what the names ‘a’, ‘b’, ‘c’ reference;
those names continue to reference whatever they were already
referencing. It is changing only what the list items ‘slist’,
‘slist’, ‘slist’ reference.
> I would also recommend reading
> http://effbot.org/zone/call-by-object.htm for a good explanation of
> how Python's variables work.
Yes, the Effbot's explanations are good.
It's also worth noting that the Python reference documentation has
become much more readable recently, and the language reference has good
explanations of the data model
and what assignment statements do
\ “As we enjoy great advantages from the inventions of others, we |
`\ should be glad to serve others by any invention of ours; and |
_o__) this we should do freely and generously.” —Benjamin Franklin |
More information about the Python-list