M
Mike Meyer
Steven D'Aprano said:Mike, you are wrong. Not about talk about "assigning to variables"Mike said:Except this doesn't have *anything at all* to do with python beingSteven D'Aprano said: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.
(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.
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.
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*.
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