Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing values in Python [duplicate]

When you pass a collection like list, array to another function in python, does it make a copy of it, or is it just a pointer?

like image 566
Joan Venge Avatar asked Feb 10 '09 21:02

Joan Venge


People also ask

Does passing by value make a copy?

By definition, pass by value means you are making a copy in memory of the actual parameter's value that is passed in, a copy of the contents of the actual parameter.

How do you pass a value in Python?

Passing Arguments in Python. Python passes arguments by assignment. That is, when you call a Python function, each function argument becomes a variable to which the passed value is assigned.

Does Python pass by reference or copy?

All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function.

Is pass useless in Python?

In Python, the pass keyword is an entire statement in itself. This statement doesn't do anything: it's discarded during the byte-compile phase. But for a statement that does nothing, the Python pass statement is surprisingly useful. Sometimes pass is useful in the final code that runs in production.


2 Answers

Python passes references-to-objects by value.

Python passes references-to-objects by value (like Java), and everything in Python is an object. This sounds simple, but then you will notice that some data types seem to exhibit pass-by-value characteristics, while others seem to act like pass-by-reference... what's the deal?

It is important to understand mutable and immutable objects. Some objects, like strings, tuples, and numbers, are immutable. Altering them inside a function/method will create a new instance and the original instance outside the function/method is not changed. Other objects, like lists and dictionaries are mutable, which means you can change the object in-place. Therefore, altering an object inside a function/method will also change the original object outside.

like image 188
Stephen Avatar answered Oct 15 '22 09:10

Stephen


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 here  func(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.

like image 34
nosklo Avatar answered Oct 15 '22 11:10

nosklo