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

Steven D'Aprano steve at REMOVEMEcyber.com.au
Wed Jan 4 21:00:54 EST 2006


Mike Meyer wrote:

> Steven D'Aprano <steve at REMOVETHIScyber.com.au> writes:
> 
>>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.
> 
> 
> Except this doesn't have *anything at all* to do with python being (or
> not being) call by reference. This is a confusion about name binding
> vs. assignment to a variable. The proper people to slap around for
> this case are the ones who talk about assignment to a variable.

Mike, you are wrong. Not about talk about "assigning to 
variables" being harmful, I agree with that. But this 
confusion is *exactly* about the call by reference 
misunderstanding. How do I know this?

Because the Original Poster said so! He said, to 
paraphrase, "Hey, I thought Python was call by 
reference, but I tried this, and it didn't work, what 
gives???"

This is not an isolated case either.

You can't get any clearer than that. Talk about call by 
reference leads to confusion and incorrect assumptions 
about Python's behaviour. End of story.



>>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.
> 
> 
> No, you get *exactly* that behavior from call by reference when you
> start passing objects around by reference. 

Who cares what Python does internally? It is strictly 
irrelevant whether Python internally passes around 
pointers, or copies values, or sometimes one or the other.

> If I declare a C object as
> "struct foo bar" and do the C "call-by-reference" hack of passing
> &bar, I get the exact same behavior I get when I pass an object
> referenced by bar to a Python subroutine.

It is not a question of whether Python's behaviour is 
*sometimes* the same as call by reference. The question 
whether Python's behaviour is *always* the same as CBR, 
and it is not. Regardless of the reasons, Python does 
not behave like CBR in other languages, and it is 
harmful to imply that it does.

Likewise, you shouldn't argue that Python is obviously 
call by value just because *sometimes* it has the same 
behaviour as CBV.

I mean, come on! A whole bunch of people argue black 
and blue that Python "obviously" is call by reference, 
and another vocal bunch argue that it is "obviously" 
call by value. Isn't that the tiniest hint that Python 
-- not the underlying C/Java/Python/whatever 
implementation, but Python the language --- does not 
fall into either camp, but is doing something that 
sometimes looks like one and sometimes like the other?

Just google on "Python is call by value" and "Python is 
call by reference" -- including quotes -- to see this 
argument come up time and time again, year after year 
after year. And all because of some false dichotomy 
that CBR and CBV are the only two valid descriptions of 
a computer language.



-- 
Steven.




More information about the Python-list mailing list