Hi, Hugo ! <br><br>Excellent explanation. Thank you.<br><br>All the best,<br>hilton <br><br><div class="gmail_quote">On Mon, Dec 6, 2010 at 5:05 PM, Hugo Arts <span dir="ltr"><<a href="mailto:hugo.yoshi@gmail.com">hugo.yoshi@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="im">On Mon, Dec 6, 2010 at 6:09 PM, Joel Schwartz <<a href="mailto:joel@joelschwartz.com">joel@joelschwartz.com</a>> wrote:<br>
> Chris,<br>
><br>
> Can you say more about number (7) in your list? What does "pass by value"<br>
> mean and what are the alternatives?<br>
><br>
> Thanks,<br>
> Joel<br>
><br>
<br>
</div>Generally, pass-by-* refers to how the arguments to functions are treated.<br>
* In call-by-value, the value of the arguments are copied into the<br>
function. There is no way to modify variables outside of the function<br>
since you don't have access to them, only to copies. C uses this,<br>
among many others:<br>
<br>
int a = 5<br>
void func(int b) { b = 6; }<br>
func(a);<br>
a == 5; /* evaluates to true, variable outside function scope remains<br>
unchanged */<br>
<br>
The value b, inside the function, contains a copy of a. So when it is<br>
modified, the original a remains unchanged.<br>
<br>
* in call-by-reference, the function is given implicit *references* to<br>
its arguments. When modifying the variables inside of the function,<br>
the variable outside is also changed. you can simulate it in many<br>
languages by passing an expicit reference rather than an implicit one<br>
(such as C's pointers):<br>
<br>
int a = 5<br>
void func(int * b) { *b = 6; }<br>
func(&a);<br>
a == 6; /* evaluates to true. the function was able to modify a<br>
variable outside of its scope */<br>
<br>
* python uses something that wikipedia calls "call-by-sharing." It is<br>
not call-by-value, nor is it call-by-reference. It means, in short,<br>
that while the function has access to the callers, *values*, it does<br>
NOT have access to the callers *variables*. To demonstrate:<br>
<br>
a = []<br>
def f(b):<br>
b.append(1)<br>
b = [2]<br>
f(a)<br>
print a # prints "[1]"<br>
<br>
As in pass-by-reference, the function f could modify it's callers<br>
values by appending 1 to the list. However, unlike *real*<br>
pass-by-reference, when trying to *re-assign* the variable into<br>
something entirely different, there was no effect (a did not become<br>
equal to [2]).<br>
<br>
<br>
Many people call python pass-by-reference, even though this is<br>
technically incorrect. The difference comes from the semantics of<br>
variables and values. In languages such as C, a variable is an area of<br>
memory that contains something. An assignment then, copies the value<br>
on the right into the variable (memory) on the left.<br>
<br>
python doesn't have variables, but names. a name is essentially itself<br>
a reference to some *object* that lives somewhere in memory. An<br>
assignment is something completely different in this context, it<br>
merely sets the reference (variable) on the left to *point to* the<br>
object on the right. So, when evaluating function arguments, names<br>
inside the function are set to point to the *objects* supplied as<br>
arguments, (not to names!). Since we don't have access to the caller's<br>
names, python is not a true pass-by-reference language.<br>
<br>
for more on why python is neither call-by-value nor call-by-reference:<br>
<a href="http://effbot.org/zone/call-by-object.htm" target="_blank">http://effbot.org/zone/call-by-object.htm</a><br>
<br>
for more on python's variable semantics and how it differs from<br>
languages like C:<br>
<a href="http://python.net/%7Egoodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables" target="_blank">http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables</a><br>
<font color="#888888"><br>
Hugo<br>
</font><div><div></div><div class="h5">_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</div></div></blockquote></div><br>