Passing values in Python [duplicate] Passing values in Python [duplicate] python python

Passing values in Python [duplicate]


Python passes references-to-objects by value.

Python passes references-to-objects byvalue (like Java), and everything inPython is an object. This soundssimple, but then you will notice thatsome data types seem to exhibitpass-by-value characteristics, whileothers seem to act likepass-by-reference... what's the deal?

It is important to understand mutableand immutable objects. Some objects,like strings, tuples, and numbers, areimmutable. Altering them inside afunction/method will create a newinstance and the original instanceoutside the function/method is notchanged. Other objects, like listsand dictionaries are mutable, whichmeans you can change the objectin-place. Therefore, altering anobject inside a function/method willalso change the original objectoutside.


Thing is, the whole reference/value concept won't fit into python. Python has no "value" of a variable. Python has only objects and names that refer to objects.

So when you call a function and put a "name" inside the parenthesis, like this:

def func(x): # defines a function that takes an argument    ... # do something herefunc(myname) # calling the function

The actual object that myname is pointing is passed, not the name myname itself. Inside the function another name (x) is given to refer to the same object passed.

You can modify the object inside the function if it is mutable, but you can't change what the outside name is pointing to. Just the same that happens when you do

anothername = myname

Therefore I can answer your question with:

it is "pass by value" but all values are just references to objects.


Answers here have been helpful, but I find the need to exhibit this fine distinction which I haven't seen covered, which I've proven to myself with the subsequent CL experiment:

  1. An immutable object ALONE CANNOT be changed within a function call. (answers so far have said that much...)
  2. BUT, an immutable object CONTAINED WITHIN a mutable object CAN be re-assigned within a method call.

'num' does not change here because it is an immutable Number object [supports my point 1.]:

def incr_num(num):    num += 1num = 0num0incr_num(num)num0

'list[0]' here is an immutable Number object also.

def incr_list(list):    list[0] += 1list = [0]list[0]0incr_list(list)list[0]1

So how did 'list[0]', being an immutable Number object, change (supports my point 2.) while the above example's Number object 'num' did not? The immutable Number object 'list[0]' is contained within the mutable list object 'list', while 'num' from the 1st example is just a non-contained Number object (immutable).

Although well-intended, I feel @Stephen Pape top-rated answer (quoted below), and some other similar ones, were not totally correct (and that motivated me to write this answer):

Some objects, like strings, tuples, and numbers, are immutable.Altering them inside a function/method will create a new instance andthe original instance outside the function/method is not changed.

My 2nd code experiment above shows a Number object ('list[0]') being altered within a method, and then the original instance outside the function changed.