This is asked very frequently. The best explanation is here: https://nedbatchelder.com/text/names.html Note that this behaviour is not specific to Python: many modern languages, such as Java, JS, Ruby, and probably more, work the same way. Answer from danielroseman on reddit.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › pass-by-reference-vs-value-in-python
Pass by reference vs value in Python - GeeksforGeeks
October 4, 2025 - In pass-by-value, a copy of the variable is passed, so changes inside the function don't affect the original. While Python doesn't strictly follow this model, immutable objects like int, str, and tuple behave similarly, as changes create new ...
🌐
Reddit
reddit.com › r/learnpython › how is python pass by reference different from the original "pass by reference" concapt?
r/learnpython on Reddit: How is python pass by reference different from the original "pass by reference" concapt?
February 27, 2022 -

Trying to understand how Python works passing arguments in functions. I've heard that Python's approach is referred to as "pass by assignment" or "pass by object reference".

How does this differ from the more traditional "pass by reference" approach? Hopefully someone can explain it in a way that's easy to understand.

Discussions

Python is not pass-by-value, nor is it pass-by-reference. It's actually pass-by-assignment.
The OP is plain wrong. All variables are references in Python, and a function call assigns the arguments to the parameters. If the argument is a constant, the parameter refers to the argument object. If the argument is a variable, the parameter will refer to the same object as the argument. The primitive data types in Python are immutable, which is why reassignment of an int changes the reference and not the object. I teach introductory Python courses and this is about the first concept that is introduced, because it is so fundamental to understanding Python. Also, it is quite different from other programming languages. More on reddit.com
🌐 r/Python
25
0
November 4, 2021
python - How do I pass a variable by reference? - Stack Overflow
As strings are immutable, they ... the same value. ... The code in BlairConrad's answer is good, but the explanation provided by DavidCournapeau and DarenThomas is correct. ... Before reading the selected answer, please consider reading this short text Other languages have "variables", Python has "names". Think about "names" and "objects" instead of "variables" and "references" and you should avoid a lot of similar problems. ... Arguments are passed by ... More on stackoverflow.com
🌐 stackoverflow.com
Basic Python - Are values passed by reference or not?
Hi, I have a basic python question: does python functions passes values by reference or not? The reason to ask is below. The function below cannot modify point and thus the output is the same point3d(0,0,0) #RhinoCommon library import Rhino.Geometry as r def change_point(pt): pt = ... More on discourse.mcneel.com
🌐 discourse.mcneel.com
9
0
April 18, 2022
variables - Python functions call by reference - Stack Overflow
In some languages you can pass a parameter by reference or value by using a special reserved word like ref or val. When you pass a parameter to a Python function it never alters the value of the parameter on leaving the function.The only way to do this is by using the global reserved word (or ... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 8
50

There is no pythonic way of doing this.

Python provides very few facilities for enforcing things such as private or read-only data. The pythonic philosophy is that "we're all consenting adults": in this case this means that "the function shouldn't change the data" is part of the spec but not enforced in the code.


If you want to make a copy of the data, the closest you can get is your solution. But copy.deepcopy, besides being inefficient, also has caveats such as:

Because deep copy copies everything it may copy too much, e.g., administrative data structures that should be shared even between copies.

[...]

This module does not copy types like module, method, stack trace, stack frame, file, socket, window, array, or any similar types.

So i'd only recommend it if you know that you're dealing with built-in Python types or your own objects (where you can customize copying behavior by defining the __copy__ / __deepcopy__ special methods, there's no need to define your own clone() method).

2 of 8
41

You can make a decorator and put the cloning behaviour in that.

>>> def passbyval(func):
def new(*args):
    cargs = [deepcopy(arg) for arg in args]
    return func(*cargs)
return new

>>> @passbyval
def myfunc(a):
    print a

>>> myfunc(20)
20

This is not the most robust way, and doesn't handle key-value arguments or class methods (lack of self argument), but you get the picture.

Note that the following statements are equal:

@somedecorator
def func1(): pass
# ... same as ...
def func2(): pass
func2 = somedecorator(func2)

You could even have the decorator take some kind of function that does the cloning and thus allowing the user of the decorator to decide the cloning strategy. In that case the decorator is probably best implemented as a class with __call__ overridden.

🌐
Robert Heaton
robertheaton.com › 2014 › 02 › 09 › pythons-pass-by-object-reference-as-explained-by-philip-k-dick
Is Python pass-by-reference or pass-by-value? | Robert Heaton
The two most widely known and easy to understand approaches to parameter passing amongst programming languages are pass-by-reference and pass-by-value.
🌐
Medium
medium.com › @devyjoneslocker › understanding-pythons-pass-by-assignment-in-the-backdrop-of-pass-by-value-vs-9f5cc602f943
Python : What is it? Pass by Value or Pass by Reference? It is Pass by Assignment | Medium
June 10, 2023 - ... On the other hand, in languages ... impact the original object itself. ... Python’s behavior is neither purely pass-by value nor pass-by-reference....
🌐
The Python Coding Stack
thepythoncodingstack.com › p › python-pass-by-value-reference-assignment
If You Haven't Got A Clue What "Pass By Value" or "Pass By Reference" mean, read on…
August 20, 2024 - So, why was the identity of the object within the function the same as the object in the global scope in one of the calls to get_the_recipe() but different in the other? Python doesn't use 'pass by reference' or 'pass by value'.
🌐
Mathspp
mathspp.com › blog › pydonts › pass-by-value-reference-and-assignment
Pass-by-value, reference, and assignment | Pydon't 🐍 | mathspp
November 3, 2021 - This means that you can modify the arguments however you please and that you won't be able to alter the state of the program outside the function. This is not what Python does, Python does not use the pass-by-value model.
Find elsewhere
🌐
Real Python
realpython.com › python-pass-by-reference
Pass by Reference in Python: Background and Best Practices – Real Python
October 21, 2023 - Some languages handle function arguments as references to existing variables, which is known as pass by reference. Other languages handle them as independent values, an approach known as pass by value.
🌐
GeeksforGeeks
geeksforgeeks.org › is-python-call-by-reference-or-call-by-value
Is Python call by reference or call by value - GeeksforGeeks
December 14, 2023 - If you pass arguments like whole numbers, strings, or tuples to a function, the passing is like a call-by-value because you can not change the value of the immutable objects being passed to the function.
🌐
Reddit
reddit.com › r/python › python is not pass-by-value, nor is it pass-by-reference. it's actually pass-by-assignment.
r/Python on Reddit: Python is not pass-by-value, nor is it pass-by-reference. It's actually pass-by-assignment.
November 4, 2021 - Python doesn't have a way to take a reference to a variable. There is no way a function can cause a value to get assigned to a local variable in the caller. ... I find it easier to name it pass by object reference, as that won't lull people ...
Top answer
1 of 16
3595

Arguments are passed by assignment. The rationale behind this is twofold:

  1. the parameter passed in is actually a reference to an object (but the reference is passed by value)
  2. some data types are mutable, but others aren't

So:

  • If you pass a mutable object into a method, the method gets a reference to that same object and you can mutate it to your heart's delight, but if you rebind the reference in the method, the outer scope will know nothing about it, and after you're done, the outer reference will still point at the original object.

  • If you pass an immutable object to a method, you still can't rebind the outer reference, and you can't even mutate the object.

To make it even more clear, let's have some examples.

List - a mutable type

Let's try to modify the list that was passed to a method:

def try_to_change_list_contents(the_list):
    print('got', the_list)
    the_list.append('four')
    print('changed to', the_list)

outer_list = ['one', 'two', 'three']

print('before, outer_list =', outer_list)
try_to_change_list_contents(outer_list)
print('after, outer_list =', outer_list)

Output:

before, outer_list = ['one', 'two', 'three']
got ['one', 'two', 'three']
changed to ['one', 'two', 'three', 'four']
after, outer_list = ['one', 'two', 'three', 'four']

Since the parameter passed in is a reference to outer_list, not a copy of it, we can use the mutating list methods to change it and have the changes reflected in the outer scope.

Now let's see what happens when we try to change the reference that was passed in as a parameter:

def try_to_change_list_reference(the_list):
    print('got', the_list)
    the_list = ['and', 'we', 'can', 'not', 'lie']
    print('set to', the_list)

outer_list = ['we', 'like', 'proper', 'English']

print('before, outer_list =', outer_list)
try_to_change_list_reference(outer_list)
print('after, outer_list =', outer_list)

Output:

before, outer_list = ['we', 'like', 'proper', 'English']
got ['we', 'like', 'proper', 'English']
set to ['and', 'we', 'can', 'not', 'lie']
after, outer_list = ['we', 'like', 'proper', 'English']

Since the the_list parameter was passed by value, assigning a new list to it had no effect that the code outside the method could see. The the_list was a copy of the outer_list reference, and we had the_list point to a new list, but there was no way to change where outer_list pointed.

String - an immutable type

It's immutable, so there's nothing we can do to change the contents of the string

Now, let's try to change the reference

def try_to_change_string_reference(the_string):
    print('got', the_string)
    the_string = 'In a kingdom by the sea'
    print('set to', the_string)

outer_string = 'It was many and many a year ago'

print('before, outer_string =', outer_string)
try_to_change_string_reference(outer_string)
print('after, outer_string =', outer_string)

Output:

before, outer_string = It was many and many a year ago
got It was many and many a year ago
set to In a kingdom by the sea
after, outer_string = It was many and many a year ago

Again, since the the_string parameter was passed by value, assigning a new string to it had no effect that the code outside the method could see. The the_string was a copy of the outer_string reference, and we had the_string point to a new string, but there was no way to change where outer_string pointed.

I hope this clears things up a little.

EDIT: It's been noted that this doesn't answer the question that @David originally asked, "Is there something I can do to pass the variable by actual reference?". Let's work on that.

How do we get around this?

As @Andrea's answer shows, you could return the new value. This doesn't change the way things are passed in, but does let you get the information you want back out:

def return_a_whole_new_string(the_string):
    new_string = something_to_do_with_the_old_string(the_string)
    return new_string

# then you could call it like
my_string = return_a_whole_new_string(my_string)

If you really wanted to avoid using a return value, you could create a class to hold your value and pass it into the function or use an existing class, like a list:

def use_a_wrapper_to_simulate_pass_by_reference(stuff_to_change):
    new_string = something_to_do_with_the_old_string(stuff_to_change[0])
    stuff_to_change[0] = new_string

# then you could call it like
wrapper = [my_string]
use_a_wrapper_to_simulate_pass_by_reference(wrapper)

do_something_with(wrapper[0])

Although this seems a little cumbersome.

2 of 16
909

The problem comes from a misunderstanding of what variables are in Python. If you're used to most traditional languages, you have a mental model of what happens in the following sequence:

a = 1
a = 2

You believe that a is a memory location that stores the value 1, then is updated to store the value 2. That's not how things work in Python. Rather, a starts as a reference to an object with the value 1, then gets reassigned as a reference to an object with the value 2. Those two objects may continue to coexist even though a doesn't refer to the first one anymore; in fact they may be shared by any number of other references within the program.

When you call a function with a parameter, a new reference is created that refers to the object passed in. This is separate from the reference that was used in the function call, so there's no way to update that reference and make it refer to a new object. In your example:

def __init__(self):
    self.variable = 'Original'
    self.Change(self.variable)

def Change(self, var):
    var = 'Changed'

self.variable is a reference to the string object 'Original'. When you call Change you create a second reference var to the object. Inside the function you reassign the reference var to a different string object 'Changed', but the reference self.variable is separate and does not change.

The only way around this is to pass a mutable object. Because both references refer to the same object, any changes to the object are reflected in both places.

def __init__(self):         
    self.variable = ['Original']
    self.Change(self.variable)

def Change(self, var):
    var[0] = 'Changed'
🌐
Blogger
stupidpythonideas.blogspot.com › 2013 › 11 › does-python-pass-by-value-or-by.html
Stupid Python Ideas: Does Python pass by value, or by reference?
November 21, 2013 - Does Python pass by value, or pass by reference? Neither. If you twist around how you interpret the terms, you can call it either. Java ...
🌐
TutorialsPoint
tutorialspoint.com › article › pass-by-reference-vs-value-in-python
Pass by reference vs value in Python
3 weeks ago - In Python Call by Value and Call by Reference are two types of generic methods to pass parameters to a function. In the Call-by-value method, the original value cannot be changed, whereas in Call-by-reference, the original value can be changed.
🌐
LinkedIn
linkedin.com › pulse › understanding-pass-by-value-vs-pass-by-reference-elhousieny-phdᴬᴮᴰ
Understanding Pass-by-Value vs. Pass-by-Reference and Mutable vs. Immutable in Python
August 20, 2023 - In pass-by-value, the value of a variable is copied and passed to a function. This is how Python behaves with immutable objects, like integers, floats, strings, and tuples.
🌐
Python Guides
pythonguides.com › python-pass-by-reference-or-value
Python Pass By Reference Or Value With Examples
September 6, 2025 - Python doesn’t strictly use call by value or call by reference. Instead, Python uses something called call by object reference (also known as call by sharing). ... When you pass an object to a function, Python passes the reference to that object, not the actual object itself.
Top answer
1 of 12
224

There are essentially three kinds of 'function calls':

  • Pass by value
  • Pass by reference
  • Pass by object reference

Python is a pass by object reference programming language.

Firstly, it is important to understand that a variable, and the value of the variable (the object) are two separate things. The variable 'points to' the object. The variable is not the object. Again:

THE VARIABLE IS NOT THE OBJECT

Example: in the following line of code:

>>> x = []

[] is the empty list, x is a variable that points to the empty list, but x itself is not the empty list.

Consider the variable (x, in the above case) as a box, and 'the value' of the variable ([]) as the object inside the box.

Pass by object reference (Case in python)

Here, "Object references are passed by value."

def append_one(li):
    li.append(1)
x = [0]
append_one(x)
print x

Here, the statement x = [0] makes a variable x (box) that points towards the object [0].

On the function being called, a new box li is created. The contents of li are the SAME as the contents of the box x. Both the boxes contain the same object. That is, both the variables point to the same object in memory. Hence, any change to the object pointed at by li will also be reflected by the object pointed at by x.

In conclusion, the output of the above program will be:

[0, 1]

Note:

If the variable li is reassigned in the function, then li will point to a separate object in memory. x however, will continue pointing to the same object in memory it was pointing to earlier.

Example:

def append_one(li):
    li = [0, 1]
x = [0]
append_one(x)
print x

The output of the program will be:

[0]

Pass by reference

The box from the calling function is passed on to the called function. Implicitly, the contents of the box (the value of the variable) are passed on to the called function. Hence, any change to the contents of the box in the called function will be reflected in the calling function.

Pass by value

A new box is created in the called function, and copies of contents of the box from the calling function are stored into the new boxes.

2 of 12
89

You can not change an immutable object, like str or tuple, inside a function in Python, but you can do things like:

def foo(y):
  y[0] = y[0]**2

x = [5]
foo(x)
print x[0]  # prints 25

That is a weird way to go about it, however, unless you need to always square certain elements in an array.

Note that in Python, you can also return more than one value, making some of the use cases for pass by reference less important:

def foo(x, y):
   return x**2, y**2

a = 2
b = 3
a, b = foo(a, b)  # a == 4; b == 9

When you return values like that, they are being returned as a Tuple which is in turn unpacked.

edit: Another way to think about this is that, while you can't explicitly pass variables by reference in Python, you can modify the properties of objects that were passed in. In my example (and others) you can modify members of the list that was passed in. You would not, however, be able to reassign the passed in variable entirely. For instance, see the following two pieces of code look like they might do something similar, but end up with different results:

def clear_a(x):
  x = []

def clear_b(x):
  while x: x.pop()

z = [1,2,3]
clear_a(z) # z will not be changed
clear_b(z) # z will be emptied
🌐
Sololearn
sololearn.com › en › Discuss › 2259297 › python-pass-by-value-and-pass-by-reference
python pass by value and pass by reference | Sololearn: Learn to code for FREE!
In Python, arguments are *always* passed by reference. You can check this by calling the id function on a value outside and inside of the function - it's the same. Try this piece of code: def f(x): print('Inside:', id(x)) a = 42 b = 'Hello' ...