Skip to content

Commit 7e4176e

Browse files
author
OpenClaw Bot
committed
docs: clarify call by value vs reference in Tutorial 4.8
Fixes #148542
1 parent 4286227 commit 7e4176e

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

Doc/tutorial/controlflow.rst

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,24 @@ referenced.
512512

513513
The actual parameters (arguments) to a function call are introduced in the local
514514
symbol table of the called function when it is called; thus, arguments are
515-
passed using *call by value* (where the *value* is always an object *reference*,
516-
not the value of the object). [#]_ When a function calls another function,
517-
or calls itself recursively, a new
515+
always passed using *call by value*. For objects, that value is an object
516+
*reference*, not the value of the object itself. This means that mutations to
517+
a mutable object made inside the function are visible outside, but reassigning
518+
the parameter to a different object does not affect the caller::
519+
520+
>>> def try_to_modify(n, l):
521+
... n = 42
522+
... l.append(42)
523+
...
524+
>>> num = 0
525+
>>> lst = []
526+
>>> try_to_modify(num, lst)
527+
>>> num
528+
0
529+
>>> lst
530+
[42]
531+
532+
When a function calls another function, or calls itself recursively, a new
518533
local symbol table is created for that call.
519534

520535
A function definition associates the function name with the function object in
@@ -1151,6 +1166,4 @@ extracted for you:
11511166

11521167
.. rubric:: Footnotes
11531168

1154-
.. [#] Actually, *call by object reference* would be a better description,
1155-
since if a mutable object is passed, the caller will see any changes the
1156-
callee makes to it (items inserted into a list).
1169+

0 commit comments

Comments
 (0)