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

Mike Meyer mwm at mired.org
Wed Jan 4 22:09:16 EST 2006


Steven D'Aprano <steve at REMOVEMEcyber.com.au> writes:
> 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???"

And he's right, and you're wrong. Look at the *code*. There isn't a
single call in it. He may have said "call by reference", but his
example code didn't have anything to do with call by rereference, and
he didn't demonstrate any confusion about the semantics of parameter
passing. He *did* demonstrate confusion about the semantics of the
assignment statement.

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

Who said anything about what Python does internally? I agree, it's
strictly irrelevant. What matters is the *behavior*.

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

You're wrong. Python's behavior is *always* the same as CBR, once you
identify what Python is passing a reference to.

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

Actually, it's either always the same as CBV, or never the same as
CBV. It's always the same as CBV because you always pass a value - and
the value is always a reference. But every calling mechanism requires
that you always pass a value. It's what the value is that determines
the calling mechanism. You pass a reference for CBR, and a name for
call by name.

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

I don't think I've seen anyone arguing that Python is
call-by-value. There are people who argue that it's sometimes one and
soemtimes the other, but that's mostly because they have incorrectly
identified what Python is passing a reference to.

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

I tried the google, and I don't see a lot of arguments. I see people
declaiming one way or another, and not very many of those. I don't
think it's an apparent dichotomy has much to do with it. I know there
are other things than CBV and CBR, and even understand Jensen's
device.

      <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list