Linked Questions
732 questions linked to/from How do I pass a variable by reference?
112
votes
12
answers
210k
views
Python functions call by reference [duplicate]
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 ...
147
votes
13
answers
205k
views
Passing an integer by reference in Python [duplicate]
How can I pass an integer by reference in Python?
I want to modify the value of a variable that I am passing to the function. I have read that everything in Python is pass by value, but there has to ...
158
votes
7
answers
154k
views
python pandas dataframe, is it pass-by-value or pass-by-reference [duplicate]
If I pass a dataframe to a function and modify it inside the function, is it pass-by-value or pass-by-reference?
I run the following code
a = pd.DataFrame({'a':[1,2], 'b':[3,4]})
def letgo(df):
...
80
votes
8
answers
99k
views
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?
42
votes
6
answers
60k
views
When is a variable passed by reference and when by value? [duplicate]
My code:
locs = [ [1], [2] ]
for loc in locs:
loc = []
print locs
# prints => [ [1], [2] ]
Why is loc not reference of elements of locs?
In Python, everything is passed as reference unless ...
15
votes
3
answers
11k
views
Understanding Python's call-by-object style of passing function arguments [duplicate]
I am not sure I understand the concept of Python's call by object style of passing function arguments (explained here http://effbot.org/zone/call-by-object.htm). There don't seem to be enough examples ...
9
votes
1
answer
57k
views
How to change the scope of a variable in a function? Python [duplicate]
This may seem like a really stupid question but I am confused regarding the scope rules in Python. In the following example I send two variables (x,y) with values to a function which is supposed to ...
6
votes
7
answers
12k
views
Change string value by function [duplicate]
I noticed that my code has many statements like this:
var = "some_string"
var = some_func(var)
var = another_func(var)
print(var) # outputs "modified_string"
It's really annoying ...
7
votes
2
answers
14k
views
Without pointers, can I pass references as arguments in Python? [duplicate]
Since Python doesn't have pointers, I am wondering how I can pass a reference to an object through to a function instead of copying the entire object. This is a very contrived example, but say I am ...
7
votes
2
answers
50k
views
Python: Keep changes on a variable made within a function [duplicate]
I have a question on a fairly simple task in python, however, I didn't manage to find a solution. I would like to assign a new value to an already existing variable in python. However, the changes I ...
18
votes
1
answer
5k
views
Python and Java parameter passing [duplicate]
I've seen in several places, including the Python documentation that Python uses pass by "assignment" semantics. Coming from a Java background, where the common mistake of saying "Java passes ...
10
votes
3
answers
5k
views
How does python assign values after assignment operator [duplicate]
Okay a very silly question I'm sure. But how does python assign value to variables?
Say there is a variable a and is assigned the value a=2. So python assigns a memory location to the variable and a ...
6
votes
4
answers
788
views
How Does Calling Work In Python? [duplicate]
For a project I'm working on, I'm implementing a linked-list data-structure, which is based on the idea of a pair, which I define as:
class Pair:
def __init__(self, name, prefs, score):
...
6
votes
1
answer
16k
views
Is list pass by value or by reference? [duplicate]
Consider the following code, at first glance it does the same thing, but the result is different, sometimes it seems list is pass by value, sometimes list seems pass by reference:
lst = [1, 2]
def f(...
7
votes
1
answer
21k
views
Incrementing integer variable of global scope in Python [duplicate]
I am trying to change global value x from within another functions scope as the following code shows,
x = 1
def add_one(x):
x += 1
then I execute the sequence of statements on Python's ...