Linked Questions
16 questions linked to/from Function changes list values and not variable values in Python
264
votes
13
answers
256k
views
Why can a function modify some arguments as perceived by the caller, but not others?
I'm trying to understand Python's approach to variable scope. In this example, why is f() able to alter the value of x, as perceived within main(), but not the value of n?
def f(n, x):
n = 2
x....
FMc's user avatar
- 42.5k
2
votes
1
answer
386
views
why python treat list and integer variable in different way? [duplicate]
Example1:
x =[]
def func():
print('x value is:', x)
x.append('member1')
print('x value is now:', x)
func()
print('x value is', x)
After run, the result will
('x value is:', [])
('x ...
0
votes
1
answer
154
views
Python - Why is my locally defined linked list updated when I call other functions that doesn't return anything [duplicate]
The code below imports a linked list from LinkedQfile and creates a list object with some node objects.
If I run this code the output from check_something() becomes CD .
I thought linked_list in ...
0
votes
0
answers
58
views
Python global vs. local variables — numbers and arrays treated differently [duplicate]
I'm learning Python, and trying to figure out why the language treats numbers and arrays differently as global vs. local variables.
Here's an example: when I write a function to increment a numeric ...
0
votes
0
answers
57
views
Function that modifies an argument outside that function without using "return" python [duplicate]
I use python 2.7.
Say I have an array X and a function that centers this array according to means :
import numpy as np
X = np.array([[float(i+j) for i in range(3)] for j in range(3)])
If we print X ...
2
votes
0
answers
45
views
Why function changes global list and not global integer with the same code [duplicate]
Why in the first code the function update created a local variable with the same name (integ) without affecting the global integ while in the second code it changed the global value of list and didn't ...
1
vote
2
answers
2k
views
Print function calling functions together
I was testing with something and I wrote this code for example purposes.I felt the output I got was weird.I expected that function calls are executed one after the other but according to this code ...
2
votes
2
answers
449
views
Python changing variables vs arrays in functions?
I am a bit confused about, changing (mutating or appending) variables from within functions.
For reference, i found this question about functions that mutate the argument. which describes doing just ...
0
votes
1
answer
828
views
How to properly implement trees with n-branching factor in python?
I am trying to implement a minimax algorithm in python, but I am having some issues creating the branching tree structure. I am still an amateur programmer so please don't mind if my code seems bad. ...
3
votes
3
answers
199
views
changing elements of a list vs changing the list itself
I have a question about the name scope of variables, I've tried to find the answer here and I got a similar one but still little confused here
Function changes list values and not variable values in ...
2
votes
4
answers
108
views
Why does this function return a value when there is no call to return?
I have the following example in my book, which is supposed to be a program whch calculates the new balance after interest is added on multiple accounts:
def addInterest(balances, rate):
for i in ...
0
votes
1
answer
74
views
Python: local, global variables
I am making new program in Python (Mastermind). I have a problem with references of variables:
def user_turn():
try_counter = 1
user_code = []
guessed_code = random_code()
print("...
-1
votes
1
answer
58
views
Why does this function modify a variable outside of it? [duplicate]
I've got this function that removes the second data value from a list (simplified). However when executing the function, the original list seems to get modified even though I'm only doing something to ...
0
votes
1
answer
47
views
why cant i get the result in the first function in the code below but have no issues getting them in the second one?
Function 1
def scale(data, factor):
for j in data:
j = j * factor
target = [1,2,3,4,5]
factors = 4
scale(target, factors)
print(target)
Output = [1, 2, 3, 4, 5]
Function 2
def scale(...
1
vote
1
answer
30
views
def loop only change list in local scope [duplicate]
In Python 3, I want to define a function that takes in a list a, does some operations and then returns some property of a.
The problem is that I want a to stay unchanged.
Take for example the ...