Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Simple object reference
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Chris Rebert  
View profile  
 More options Nov 14, 6:40 pm
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Sat, 14 Nov 2009 15:40:05 -0800
Local: Sat, Nov 14 2009 6:40 pm
Subject: Re: Simple object reference

On Sat, Nov 14, 2009 at 3:25 PM, AON LAZIO <aonla...@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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Finney  
View profile  
 More options Nov 14, 7:36 pm
Newsgroups: comp.lang.python
From: Ben Finney <ben+pyt...@benfinney.id.au>
Date: Sun, 15 Nov 2009 11:36:25 +1100
Local: Sat, Nov 14 2009 7:36 pm
Subject: Re: Simple object reference

Chris Rebert <c...@rebertia.com> writes:
> On Sat, Nov 14, 2009 at 3:25 PM, AON LAZIO <aonla...@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[0]’ 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[0]’,
‘slist[1]’, ‘slist[2]’ 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
<URL:http://docs.python.org/reference/datamodel.html#objects-values-and-types>
and what assignment statements do
<URL:http://docs.python.org/reference/simple_stmts.html#assignment-statements>.

--
 \     “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 |
Ben Finney


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options Nov 14, 9:53 pm
Newsgroups: comp.lang.python
From: Terry Reedy <tjre...@udel.edu>
Date: Sat, 14 Nov 2009 21:53:39 -0500
Local: Sat, Nov 14 2009 9:53 pm
Subject: Re: Simple object reference

Chris Rebert wrote:
> On Sat, Nov 14, 2009 at 3:25 PM, AON LAZIO <aonla...@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.

That is not right either, or else newbies would not be surprised by
 >>> a = [0]
 >>> b = [a]
 >>> b[0][0] = 1
 >>> a
[1]

Subscriptable collections associate subscripts with objects.
Namespaces associated names with objects.
In either case, if you change the object, you change it, regardless of
how you access it, such as by means of other associations.
If you replace an association by associating a name or subscript with a
new object, the old object is untouched (unless that was its last
association) and other access methods by means of other associations are
not affected.

Terry Jan Reedy


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Nov 14, 10:19 pm
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Sat, 14 Nov 2009 19:19:19 -0800
Local: Sat, Nov 14 2009 10:19 pm
Subject: Re: Simple object reference

Okay, I should have technically said "references to objects" rather
than "values", but in any case, names themselves are certainly not
referenced or the following would print "[1]".

>>> a = [0]
>>> b = [a]
>>> a = [42]
>>> b[0][0] = 1
>>> a

[42]

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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google