Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Some answers contain the word "copy" in a context of a function call. I find it confusing.</p> <p><strong>Python doesn't copy <em>objects</em> you pass during a function call <em>ever</em>.</strong></p> <p>Function parameters are <em>names</em>. When you call a function Python binds these parameters to whatever objects you pass (via names in a caller scope).</p> <p>Objects can be mutable (like lists) or immutable (like integers, strings in Python). Mutable object you can change. You can't change a name, you just can bind it to another object.</p> <p>Your example is not about <a href="https://docs.python.org/2/tutorial/classes.html#python-scopes-and-namespaces" rel="noreferrer">scopes or namespaces</a>, it is about <a href="http://docs.python.org/reference/executionmodel.html#naming-and-binding" rel="noreferrer">naming and binding</a> and <a href="http://docs.python.org/reference/datamodel.html#objects-values-and-types" rel="noreferrer">mutability of an object</a> in Python. </p> <pre><code>def f(n, x): # these `n`, `x` have nothing to do with `n` and `x` from main() n = 2 # put `n` label on `2` balloon x.append(4) # call `append` method of whatever object `x` is referring to. print 'In f():', n, x x = [] # put `x` label on `[]` ballon # x = [] has no effect on the original list that is passed into the function </code></pre> <p>Here are nice pictures on <a href="https://web.archive.org/web/20180121150727/http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables" rel="noreferrer">the difference between variables in other languages and names in Python</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload