Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Is 'everything' a refrence or isn't it?

Reply
Thread Tools

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

 
 
KraftDiner
Guest
Posts: n/a
 
      01-04-2006
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.)
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?

 
Reply With Quote
 
 
 
 
Christian Tismer
Guest
Posts: n/a
 
      01-04-2006
KraftDiner wrote:
> I was under the assumption that everything in python was a refrence...


This is true.

> so if I code this:
> lst = [1,2,3]


lst is a reference to a list that holds references to 1, 2, and 3

> for i in lst:


i is a reference to every element in the list, one ofter the other.
But this is not a ref to lst any longer, the list gets dereferenced
before getting at its contents, and only references to that are
returned.

> if i==2:
> i = 4


You now have i as a reference to 4, during this cycle.

ciao - chris

--
Christian Tismer :^) <(E-Mail Removed)>
tismerysoft GmbH : Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9A : *Starship* http://starship.python.net/
14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
work +49 30 802 86 56 mobile +49 173 24 18 776 fax +49 30 80 90 57 05
PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
whom do you want to sponsor today? http://www.stackless.com/
 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      01-04-2006
On 2006-01-04, KraftDiner <(E-Mail Removed)> 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!!
 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      01-04-2006
KraftDiner wrote:

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


It is, although it is better to think in terms of names and bindings.

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


During execution of this code, the name 'i' is bound to 1, 2, 3 and 4. the
list elements are bound to 1, 2, and 3. Rebinding 'i' such that instead of
referencing 2 it now references 4 doesn't affect the fact that the second
element of the list references 2.

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


Evidently.
 
Reply With Quote
 
Scott David Daniels
Guest
Posts: n/a
 
      01-04-2006
KraftDiner wrote:
> Have I misunderstood something?

yes.
There is a reason people talk about names and bindings.


--
-Scott David Daniels
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Simon Brunning
Guest
Posts: n/a
 
      01-04-2006
On 4 Jan 2006 10:54:17 -0800, KraftDiner <(E-Mail Removed)> wrote:
> I was under the assumption that everything in python was a refrence...

(snip)
> Have I misunderstood something?


Yup. The concept if a reference is, I find, sometimes an unhelpful one
in Python. Reset your brain -
<http://effbot.org/zone/python-objects.htm>.

Armed with that, let's go through your code:

> lst = [1,2,3]


Here, you've created a list object, and bound it to the name "lst".
The list contains references to three integer objects. (OK I said
"reference" there didn't I? Well, I only said that it's *sometimes* an
unhelpful concept. I do tend to think of collections as refering
to objects. It's when dealing with you might call variables that it's
misleading - there it's better to think of names, objects, and
bindings.)

> for i in lst:


Here, you are iterating through the list that's currently bount to the
name "lst". You are binding each element of the list in turn to the
name "i".

> if i==2:


If the object to which the name "i" is currently bound compares equal to 2....

> i = 4


Rebind the name "i" to the integer object 4. Note that this has no
effect on the object that used to be bound to "i" at this point, nor
any effect on the list object.

> print lst


Print your old list.

Clear now?

--
Cheers,
Simon B,
(E-Mail Removed),
http://www.brunningonline.net/simon/blog/
 
Reply With Quote
 
Claudio Grondi
Guest
Posts: n/a
 
      01-04-2006
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.)
> 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?
>

To give a short answer: yes.

And now into the details assuming, that your misunderstanding comes from
programming in C before evaluating Python:

If you write
i = 4
following happens:
an integer literal 4 gets into the scope of Python script as an object
of type integer and can be from now on reached using the identifier i .
So i is in this context a _kind of_ pointer/reference to the value
'4', but ... in Python sense not in C sense.

if you write
for i in lst:
then the identifier i 'points' to values in lst, but else as in C
where you can use a pointer to change the value it points to by
assigning to *i or where you can use a reference to change a value it
references, in Python you don't change a value of i because i is an
identifier and therefore has no value in sense of C - it is only a name
used to reach a value.

When you write within the 'for i in lst:' loop
i = 4
you just use the identifier i as a 'pointer' to another Python object
as it was pointing to before.

Let's consider a list.
The 'identifier'(i.e. construct) lst[i] pointing to a list element
behaves in Python like what you understand in C as a reference.

This is the main difference between the meaning of = assignment in
Python and in C and it usually results in problems with understanding it
when someone has programmed C and expects from Python to be like C.

The core of the trouble is probably different understanding of the word
'reference', which used in different contexts (e.g. Python and C) means
different things. Therefore as Scott David Daniels says
"There is a reason people talk about names and bindings."

Claudio
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-04-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.)


See, this confusion is precisely why I get the urge to slap people who
describe Python as "call by reference". It isn't.

It isn't "call by value" either -- Python never copies objects unless you
explicitly tell it to.

It is "call by object" -- you pass around *objects*. Internally, this is
quite fast, because the entire object doesn't need to be moved, only
pointers to objects, but you don't get the behaviour of either call by
reference or call by value.

In the above code, i is a name bound one at a time to the ints [1,2,3].
When you re-assign i to 4, that doesn't change the object 2 into the
object 4, because ints are immutable. Only the name i is rebound to a new
object 4. That doesn't change objects like lst which include 2 inside them.

See this for more detail:

http://effbot.org/zone/call-by-object.htm



--
Steven.

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-04-2006
On Wed, 04 Jan 2006 20:05:38 +0100, Christian Tismer wrote:

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

>
> This is true.


No it is not.

>> so if I code this:
>> lst = [1,2,3]

>
> lst is a reference to a list that holds references to 1, 2, and 3
>
>> for i in lst:

>
> i is a reference to every element in the list, one ofter the other.
> But this is not a ref to lst any longer, the list gets dereferenced
> before getting at its contents, and only references to that are
> returned.


See what confusing, complicated hoops people have to jump through to
hammer the round peg of Python's behaviour into the square peg of "call by
reference"?

http://effbot.org/zone/call-by-object.htm

Python does not have references or pointers, except internally where
Python coders can not get to them. It has names and objects. Keep thinking
about "call by reference" and you just confuse yourself and others. Think
about names and objects and it is simple and straight-forward.



--
Steven.

 
Reply With Quote
 
Claudio Grondi
Guest
Posts: n/a
 
      01-04-2006
Steven D'Aprano wrote:
> 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.)

>
>
> See, this confusion is precisely why I get the urge to slap people who
> describe Python as "call by reference". It isn't.
>
> It isn't "call by value" either -- Python never copies objects unless you
> explicitly tell it to.
>
> It is "call by object" -- you pass around *objects*. Internally, this is
> quite fast, because the entire object doesn't need to be moved, only
> pointers to objects, but you don't get the behaviour of either call by
> reference or call by value.
>
> In the above code, i is a name bound one at a time to the ints [1,2,3].
> When you re-assign i to 4, that doesn't change the object 2 into the
> object 4, because ints are immutable. Only the name i is rebound to a new
> object 4. That doesn't change objects like lst which include 2 inside them.
>
> See this for more detail:
>
> http://effbot.org/zone/call-by-object.htm
>
>

It seems to be hard to explain it all in a straighforward way without
using pictures showing what happens when an assignment is processed
demonstrating what is what and how it is called. Probably a small nice
movie could be here the right medium to be handed over to Python newbies
coming from C to give some hints towards proper understanding. Replacing
one word with another is not sufficient to avoid confusion as the
concept is probably not always easy to grasp (I have still problems to
get the idea how it works 'inside' with this local and global
dictionaries and so on).
Lack of knowledge about a good reference was the reason why I decided to
give a reply here inspite of the fact, that so many other postings were
already there, but none (including mine) which covered all aspects, so
that I could say: "Wow! It is an excelent explanation and there is
nothing which must be added to it!".
Is there anywhere an ultimative enlightening explanation of the concept
of identifiers and variables in Python for use in case the next posting
of that kind hits comp.lang.python and it sure will as they are coming
in quite regular.
http://effbot.org/zone/call-by-object.htm is sure an attempt of a good
explanation, but in my eyes still nothing what immediately enlightens.

Claudio
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
For refrence pete Computer Support 2 10-20-2004 08:57 AM
Group Policy refrence Rich MCSE 48 07-28-2004 09:39 PM
How can I refrence the aspNetEmail Mike John ASP .Net 1 07-06-2003 09:01 PM
Re: How to refrence html objec t from aspx form Kevin Spencer ASP .Net 0 06-30-2003 02:28 PM
How can I refrence html object from aspx form. Mike John ASP .Net 1 06-29-2003 12:45 AM



Advertisments