Linked Questions

112 votes
12 answers
210k views

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 ...
Timothy Lawman's user avatar
147 votes
13 answers
205k views

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 ...
CodeKingPlusPlus's user avatar
158 votes
7 answers
154k views

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): ...
nos's user avatar
  • 21.1k
80 votes
8 answers
99k views

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?
Joan Venge's user avatar
  • 334k
42 votes
6 answers
60k views

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 ...
Yugal Jindle's user avatar
15 votes
3 answers
11k views

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 ...
smilingbuddha's user avatar
9 votes
1 answer
57k views

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 ...
Reboot_87's user avatar
  • 589
6 votes
7 answers
12k views

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 ...
impress_meh's user avatar
7 votes
2 answers
14k views

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 ...
hannah's user avatar
  • 919
7 votes
2 answers
50k views

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 ...
ZeroStrikeCall's user avatar
18 votes
1 answer
5k views

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 ...
orrymr's user avatar
  • 2,523
10 votes
3 answers
5k views

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 ...
qwertp's user avatar
  • 859
6 votes
4 answers
790 views

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): ...
Pseudo-Gorgias's user avatar
6 votes
1 answer
16k views

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(...
an offer can't refuse's user avatar
7 votes
1 answer
21k views

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 ...
Pip S's user avatar
  • 133

15 30 50 per page
1
2 3 4 5
49