All variables in Python are references. Elementary data types aren't an exception.

In the first example, you reassign b. It no longer references the same object as a.

In the second example, you modify b. Since you've previously set a and b to be references to the same object, the modification applies to a as well.

Answer from Mark Ransom on Stack Overflow
🌐
W3Schools
w3schools.com › python › python_reference.asp
Python Reference
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... This section contains a Python reference documentation.
🌐
Python documentation
docs.python.org › 3 › reference › index.html
The Python Language Reference — Python 3.14.8 documentation
This reference manual describes the syntax and core semantics of the language. It is terse, but attempts to be exact and complete. Elsewhere, the built-in object types and functions are described in Python built-ins reference.
🌐
Python Tutorial
pythontutorial.net › home › advanced python › python references
Python References
March 27, 2025 - When you access the counter variable, Python looks up the object referenced by the counter and returns the value of that object: ... So variables are references that point to the objects in the memory.
🌐
GeeksforGeeks
geeksforgeeks.org › shared-reference-in-python
Shared Reference in Python | GeeksforGeeks
March 14, 2024 - x = 5 y = x When Python looks at the first statement, what it does is that, first, it creates an object to represent the value 5. Then, it creates the variable x if it doesn't exist and made it a reference to this new object 5. The second line causes Python to create the variable y, and it is not assigned with x, rather it is made to reference that object that x does. The net effect is that the variables x and y wind up referencing the same object. This situation, with multiple names referencing the same object, is called a Shared Reference in Python.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › python references
Python References | Tutorials on How References Works in Python
March 31, 2023 - A reference in python means a different name for a memory location that has been associated. This means an entity allocated with some memory will be referred to or referenced with a different name other than the actual name of the memory.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Python documentation
docs.python.org › 3 › reference
The Python Language Reference — Python 3.14.6 documentation
March 16, 2023 - This reference manual describes the syntax and “core semantics” of the language. It is terse, but attempts to be exact and complete. The semantics of non-essential built-in object types and of the built-in functions and modules are described in The Python Standard Library.
🌐
Medium
medium.com › geekculture › python-reference-e6458a9a0582
Python — Reference
August 9, 2022 - In Python, when a = 343 is executed, it first creates the object 343 in memory, and then let a point to it, which is the reference.
Find elsewhere
🌐
Runestone Academy
runestone.academy › ns › books › published › fopp › TransformingSequences › ObjectsandReferences.html
9.4. Objects and References — Foundations of Python Programming
In other words, the references are the same. Try our example from above. The answer is True. This tells us that both a and b refer to the same object, and that it is the second of the two reference diagrams that describes the relationship. Python assigns every object a unique id and when we ask a is b what python is really doing is checking to see if id(a) == id(b).
🌐
O'Reilly
oreilly.com › library › view › python-in-a › 0596001886 › ch04s03.html
Variables and Other References - Python in a Nutshell [Book]
March 3, 2003 - Variables and Other References A Python program accesses data values through references. A reference is a name that refers to the specific location in memory of a value (object).... - Selection from Python in a Nutshell [Book]
Author: Alex Martelli
Published: 2003
Pages: 656
🌐
Medium
medium.com › @dreamferus › you-need-to-understand-variables-and-references-in-python-a-guide-51f0d480c083
You need to understand variables and references in Python — A guide
October 17, 2022 - A reference can be seen as the connection between a variable/name and a value, it contains the memory address where the value is held.
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'
🌐
Codementor
codementor.io › community › variable references in python
Variable references in Python | Codementor
April 22, 2019 - In python when we assign a value to a name, we actually create an object and a reference to it. For example in a=1, an object with value '1' is created in memory and a reference 'a' now points to it.
🌐
Medium
medium.com › @officialyrohanrokade › simplified-pythons-reference-handling-understanding-object-referencing-deleting-references-and-ae4026a74d7b
Simplified Python’s Reference Handling: Understanding Object Referencing, Deleting References, and Shallow Copying vs. Deep Copying | by Rohan Rokade | Medium
December 8, 2023 - Assigning one object to the other doesn’t spawn a new object; it establishes another reference to the same object. Modifying attributes through one reference influences the shared object, impacting both references. Reference counts serve as a fundamental part of Python’s memory management.
🌐
Real Python
realpython.com › python-pass-by-reference
Pass by Reference in Python: Background and Best Practices – Real Python
March 18, 2026 - As you can see, the refParameter ... will be passed in by reference and can be modified in place. Python has no ref keyword or anything equivalent to it....
🌐
Medium
medium.com › swlh › a-deep-dive-into-variables-in-python-8f55f69c3653
A Deep Dive Into Variable References in Python | The Startup
June 14, 2020 - Instead, Python creates a new reference to an object representing that value. For example, the line a = 1 assigns the value 1 to the variable a. Behind the scenes, Python creates a new reference for a to point at the object representing the value 1.
🌐
Bite Code
bitecode.dev › bite code! › python variables, references and mutability
Python variables, references and mutability - Bite code!
September 19, 2023 - Again, many programming languages can pass things either by reference or by value. "By value" is a copy. "By reference" passes the information of where to get the thing you are talking about. In Python, there is no "by value".
Top answer
1 of 3
50

All values in Python are references. What you need to worry about is if a type is mutable. The basic numeric and string types, as well as tuple and frozenset are immutable; names that are bound to an object of one of those types can only be rebound, not mutated.

>>> t = 1, 2, 3
>>> t[1] = 42
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
2 of 3
24

Coming from iOS development using strongly typed Swift language, Python reference was a bit confusing so I decided to do a little comparison. Here is the summary: -

  • When assigning a variable to python say a = 10 you are simply pointing/referencing the the object in this case 10 which is stored in a memory. So if that object changes then the value of a variable a also changes but changing a does not change the object 10, This behave similar to Swift Primitive value types such as Int.

To make this clear here is an example: -


 # "a" points to an object in this case 10
a = 10

# "b" points to the same object which a points but does not point to a variable a.
b = a 

# Now if we change "a" to point to another object in memory say 20. 
a = 20

# "b" still points to the old object 10 in other words
# "b == 10" but "a == 20", This is because "b" was never pointing to the variable "a" 
# even though we assigned it as "b = a" instead it was pointing to the object 10
#  which is # the same as writing b = 10. 

Let's check with a more complex data structure List

list1 = [10,20,30,40]
list2 = list1 #[10,20,30,40]

list1 = [3,4] 

# list1 ==> [3,4]
# list2 ==> [10,20,30,40]


Again that behave the same to Swift and other similar languages. Here comes the huge difference Let's try changing value at a certain index ( This gets more tricky)

list1 = [10,20,30,40]
list2 = list1 #[10,20,30,40]

# change value of list 1 at a certain index say index 0
list1[0] = 500

# If you check again the values of list1 and list2 you will be surprised. 
#list1 ==> [500,20,30,40]
#list2 ==> [500,20,30,40]

They both change because they were all pointing to the same object so changing the object changes all list1 and list2. This is very confusing from other Languages such as Swift. In Swift List/Array are value types meaning they are not referenced instead they are copied around, However in python it is another story, changing a value at a certain index results in changing that value for all properties which references that object just like in the example above. This is very important to keep in mind for folks coming from Swift or other similar languages.

So how do we copy in python?

  • If you want to copy the list in python then you have to explicitly do so as shown on the example below: -
list1 = [10,20,30,40]
list2 = list(list1)

# list1 ==> [10,20,30,40]
# list2 ==> [10,20,30,40]

Doing so will avoid undesired effects when list1 changes list2 will remain the same.

As an example

list1[0] = 500
#list1 ==> [500,20,30,40] # Changed
#list2 ==> [10,20,30,40] # Unchanged