This question already has answers here:
Why can a function modify some arguments as perceived by the caller, but not others?
(13 answers)
Closed 6 months ago.
I was studying merge sort and I wrote this code
def mergesort(a): #sorting algo
#code is alright just confused about scope of variable
if(len(a)>1):
mid = len(a)//2
l = a[:mid]
r = a[mid:]
mergesort(l)
mergesort(r)
i=j=k=0
while(i<len(l) and j<len(r)):
if(l[i]>r[j]):
a[k] = r[j]
j+=1
else:
a[k] = l[i]
i+=1
k+=1
while(i<len(l)):
a[k] = l[i]
i+=1
k+=1
while(j<len(r)):
a[k] = r[j]
j+=1
k+=1
a = [30,2,4,5,-1,3,7,3,9,2,5]
mergesort(a)
print(a)
here a is a global variable, but when the function parameter name is also a doesn't the a in function scope override the a in the global scope? how the global variable is getting edited inside mergesort function?
I tried another code with same approach but here the global variable doesn't get edited(as expected),how is this different from the above code?
def foo(a):
a=20
a = 10
foo(a)
print(a)
The problem is not that a is global (because it's not). The a is local to the function, but it references the same list as the global a (because variables are passed by reference in python).
a is a list (which is mutable) in the first example, while a is an integer (which is immutable) in the second example. So changing values inside of the list (like a[k] = l[i]), changes the global list as well, while changing the reference to an integer (like a = 20) only replaces the local reference.
If you don't want the function to change the global list, you can fix it by creating a copy of the list:
def mergesort(a):
a = a[:] # or a = a.copy(), whichever you prefer
...
Note that in this case, the function has no effect, so you'd probably want to return the resulting list.
Lists are mutable but int is not. So when you change int, you get an entirely new int. When you make changes within a list, you don't get a new list. You just have a modified list.
So when you pass the list to a function, it does not create a new list unless you do something like this: a.copy() or a[:] which creates a new list. In your case, you are using the same list as outside the function.
For more info read about mutability of data types or pass by reference and pass by value.
Related
This question already has answers here:
Why can a function modify some arguments as perceived by the caller, but not others?
(13 answers)
Closed 3 years ago.
Let's take a simple code:
y = [1,2,3]
def plusOne(y):
for x in range(len(y)):
y[x] += 1
return y
print plusOne(y), y
a = 2
def plusOne2(a):
a += 1
return a
print plusOne2(a), a
Values of 'y' change but value 'a' stays the same. I have already learned that it's because one is mutable and the other is not. But how to change the code so that the function doesn't change the list?
For example to do something like that (in pseudocode for simplicity):
a = [1,2,3,...,n]
function doSomething(x):
do stuff with x
return x
b = doSomething(a)
if someOperation(a) > someOperation(b):
do stuff
EDIT: Sorry, but I have another question on nested lists. See this code:
def change(y):
yN = y[:]
for i in range(len(yN)):
if yN[i][0] == 1:
yN[i][0] = 0
else:
yN[i][0] = 1
return yN
data1 = [[1],[1],[0],[0]]
data2 = change(data1)
Here it doesn't work. Why? Again: how to avoid this problem? I understand why it is not working: yN = y[:] copies values of y to yN, but the values are also lists, so the operation would have to be doubled for every list in list. How to do this operation with nested lists?
Python variables contain pointers, or references, to objects. All values (even integers) are objects, and assignment changes the variable to point to a different object. It does not store a new value in the variable, it changes the variable to refer or point to a different object. For this reason many people say that Python doesn't have "variables," it has "names," and the = operation doesn't "assign a value to a variable," but rather "binds a name to an object."
In plusOne you are modifying (or "mutating") the contents of y but never change what y itself refers to. It stays pointing to the same list, the one you passed in to the function. The global variable y and the local variable y refer to the same list, so the changes are visible using either variable. Since you changed the contents of the object that was passed in, there is actually no reason to return y (in fact, returning None is what Python itself does for operations like this that modify a list "in place" -- values are returned by operations that create new objects rather than mutating existing ones).
In plusOne2 you are changing the local variable a to refer to a different integer object, 3. ("Binding the name a to the object 3.") The global variable a is not changed by this and continues to point to 2.
If you don't want to change a list passed in, make a copy of it and change that. Then your function should return the new list since it's one of those operations that creates a new object, and the new object will be lost if you don't return it. You can do this as the first line of the function: x = x[:] for example (as others have pointed out). Or, if it might be useful to have the function called either way, you can have the caller pass in x[:] if he wants a copy made.
Create a copy of the list. Using testList = inputList[:]. See the code
>>> def plusOne(y):
newY = y[:]
for x in range(len(newY)):
newY[x] += 1
return newY
>>> y = [1, 2, 3]
>>> print plusOne(y), y
[2, 3, 4] [1, 2, 3]
Or, you can create a new list in the function
>>> def plusOne(y):
newList = []
for elem in y:
newList.append(elem+1)
return newList
You can also use a comprehension as others have pointed out.
>>> def plusOne(y):
return [elem+1 for elem in y]
You can pass a copy of your list, using slice notation:
print plusOne(y[:]), y
Or the better way would be to create the copy of list in the function itself, so that the caller don't have to worry about the possible modification:
def plusOne(y):
y_copy = y[:]
and work on y_copy instead.
Or as pointed out by #abarnet in comments, you can modify the function to use list comprehension, which will create a new list altogether:
return [x + 1 for x in y]
Just create a new list with the values you want in it and return that instead.
def plus_one(sequence):
return [el + 1 for el in sequence]
As others have pointed out, you should use newlist = original[:] or newlist = list(original) to copy the list if you do not want to modify the original.
def plusOne(y):
y2 = list(y) # copy the list over, y2 = y[:] also works
for i, _ in enumerate(y2):
y2[i] += 1
return y2
However, you can acheive your desired output with a list comprehension
def plusOne(y):
return [i+1 for i in y]
This will iterate over the values in y and create a new list by adding one to each of them
To answer your edited question:
Copying nested data structures is called deep copying. To do this in Python, use deepcopy() within the copy module.
You can do that by make a function and call this function by map function ,
map function will call the add function and give it the value after that it will print the new value like that:
def add(x):
return x+x
print(list(map(add,[1,2,3])))
Or you can use (*range) function it is very easy to do that like that example :
print ([i+i for i in [*range (1,10)]])
I am reading Hackers and Painters and am confused by a problem mentioned by the author to illustrate the power of different programming languages.
The problem is:
We want to write a function that generates accumulators—a function that takes a number n, and returns a function that takes another number i and returns n incremented by i. (That’s incremented by, not plus. An accumulator has to accumulate.)
The author mentions several solutions with different programming languages. For example, Common Lisp:
(defun foo (n)
(lambda (i) (incf n i)))
and JavaScript:
function foo(n) { return function (i) { return n += i } }
However, when it comes to Python, the following codes do not work:
def foo(n):
s = n
def bar(i):
s += i
return s
return bar
f = foo(0)
f(1) # UnboundLocalError: local variable 's' referenced before assignment
A simple modification will make it work:
def foo(n):
s = [n]
def bar(i):
s[0] += i
return s[0]
return bar
I am new to Python. Why doesn the first solution not work while the second one does? The author mentions lexical variables but I still don't get it.
s += i is just sugar for s = s + i.*
This means you assign a new value to the variable s (instead of mutating it in place). When you assign to a variable, Python assumes it is local to the function. However, before assigning it needs to evaluate s + i, but s is local and still unassigned -> Error.
In the second case s[0] += i you never assign to s directly, but only ever access an item from s. So Python can clearly see that it is not a local variable and goes looking for it in the outer scope.
Finally, a nicer alternative (in Python 3) is to explicitly tell it that s is not a local variable:
def foo(n):
s = n
def bar(i):
nonlocal s
s += i
return s
return bar
(There is actually no need for s - you could simply use n instead inside bar.)
*The situation is slightly more complex, but the important issue is that computation and assignment are performed in two separate steps.
An infinite generator is one implementation. You can call __next__ on a generator instance to extract successive results iteratively.
def incrementer(n, i):
while True:
n += i
yield n
g = incrementer(2, 5)
print(g.__next__()) # 7
print(g.__next__()) # 12
print(g.__next__()) # 17
If you need a flexible incrementer, one possibility is an object-oriented approach:
class Inc(object):
def __init__(self, n=0):
self.n = n
def incrementer(self, i):
self.n += i
return self.n
g = Inc(2)
g.incrementer(5) # 7
g.incrementer(3) # 10
g.incrementer(7) # 17
In Python if we use a variable and pass it to a function then it will be Call by Value whatever changes you make to the variable it will not be reflected to the original variable.
But when you use a list instead of a variable then the changes that you make to the list in the functions are reflected in the original List outside the function so this is called call by reference.
And this is the reason for the second option does work and the first option doesn't.
This question already has answers here:
How do I get a result (output) from a function? How can I use the result later?
(4 answers)
Python difference between mutating and re-assigning a list ( _list = and _list[:] = )
(3 answers)
Closed 6 months ago.
I created a function and I want to append a number to the list:
def num(f):
list1.append(i)
return list1
list1 = []
i = 1
print "Now list1 is %s and i is %d" % (list1, i)
num(list1)
i += 1
print "Now list1 is %s and i is %d" % (list1, i)
num(list1)
i += 1
print "Now list1 is %s and i is %d" % (list1, i)
print list1
print i
Why do I have to return a function? It works with and without the return.
I was told that the function returns None if no return-statement was reached. But the function, mentioned above, works even if I don't type this return-statement.
I see you don't understand how functions work, so I added comments to your code to explain a little, but I suggest you to read Python tutorial about functions and wiki article further to gain understanding.
Also, I omitted many details not to overload the explanation. Important thing is there're immutable (i.e. integer, i in your example) and mutable (i.e. list, list1 in your example) types in Python and depending on this the behavior will be different.
def num(f):
#Here the argument you pass to the function is named 'f'
#and you don't use it
#The next line uses 'list1', that is defined in global scope
#since you didn't redefined this name inside the function
#Variable 'i' is also the one in global scope for same reasons
list1.append(i)
#Here you return 'list1', though you don't use this value
#further in your program. Indeed, you would not write a return
#statement the function would return 'None' as the return value
return list1
#Here you define 'list1' in global scope, and it will be used
#inside 'num' function, even without providing it as the argument
list1 = []
#Here you define 'i' in global scope, and it will be used
#inside 'num' function
i = 1
#Here you print 'i' and 'list' from global scope
print "Now list1 is %s and i is %d" % (list1, i)
#Here you call 'num' function and 'list1' provided as argument
#is assigned to 'f' inside the function, but you didn't used it and
#and instead used names from global scope - that's why it works in
#this way (however it is wrong use of function)
#With 'list1.append(i)' the 'list1' is modified in place so it
#doesn't matter if it is returned or not
num(list1)
#As to 'num' return value, it will be the same value as 'list1', but
#you don't use it here, to use it it needs to be assigned with '=' to
#some variable, i.e. 'list2=num(list1)', though in fact 'list1' and 'list2'
#will be the same all the time due to Python internals, but let's skip the
#details of this.
#You can see that the value returned is not 'None' - add the
#following line here:
print(num(list1))
#and run your program, the output will show you that it's a list returned.
#then remove the 'return' line in your function and run program again
#the output here will show, that is's 'None' that was returned.
So to fix the obvious mistake in the function:
def num(f):
f.append(i)
return f
but i is still used from global scope and not passed as argument, so even better:
def num(f_var,i_var):
f_var.append(i_var)
return f_var
Though the list will be modified inplace and you don't really have to return it
in you particular example, so:
def num(f_var,i_var):
f_var.append(i_var)
list1=[]
i=1
num(list1,i)
will work too.
In the provided example, returning a value is unnecessary because the append list method, and by extension your function as well, operates by side effect. Functions called only for side effects are not functions in the mathematical sense (mappings of domain values to codomain values), they serve to change state of mutable objects, in this case by appending an item to a list. While such functions can return a value, Python has a convention that functions invoked purely for side effect, such as list.append, only return None, denoting no useful return value.
If your function were side-effect-free, you would need to have a return statement for it to be useful at all. As an example, compare:
def add(a, b):
return a + b
...with the syntactically just as legal, but pretty useless:
def add(a, b):
a + b
# without a return statement, None is returned,
# and the calculated sum discarded
You don't use the return value, so it makes no difference, what you return. You also don't use the argument. You probably wanted to write
def append_num(f, i):
f.append(i)
and use two arguments:
append_num(list1, i)
First of all, I understand that I can use global statement to access global variables. But somehow I was able to modify a global list without global like below:
def func1(nums):
nums = [4,5,6]
nums = [1,2,3]
func1(nums)
print nums # print [1,2,3]
def func2(nums):
nums[0] = 4
nums[1] = 5
nums[2] = 6
nums = [1,2,3]
func2(nums)
print nums # print [4,5,6]
After trying func2, I realized that I can always access global list in a function if I specify the index:
def func3(nums):
nums[:] = [4,5,6]
nums = [1,2,3]
func3(nums)
print nums # print [4,5,6]
Is it because Python automatically goes trying to match a global variable if a function variable is used before definition?
I understand that I can use global statement to access global variables
Your understanding is wrong. You can always access a global variable as long as you don't have a local variable of the same name. You only need the global statement when you are going to change what object a variable name refers to. In your func2, you are not doing that; you are only changing the contents of the object. nums still refers to the same list.
It is of concept based on mutable and immutable objects in Python. In your case, for example:
a=[1,2]
def myfn():
a=[3,4]
print id(a)
>>>id(a)
3065250924L
>>>myfn()
3055359596
It is clear both are different objects. Now:
a=[1,2]
def myfn():
a[:] =[3,4]
print id(a)
>>>id(a)
3055358572
>>>myfn()
3055358572
That means it is same variable using in local and global scope.
In this specific case it is because lists are mutable.
As a result having them in the global namespace, or even passed through a function, means that they will be changed as Python holds the reference to the mutable object, not a copy of it.
If you try the same thing with tuples it will not work, as they are immutable.
The way to avoid this is to provide a copy of the list to the function, not the list itself:
func2(list[:])
At the same time you can do this with default arguments, where you can specify a default argument to be [], and if you then .append() something to it, that default argument will forever hold that item within it for all future calls (unless you remove it in some way).
2 variables nums are different and they point to a same object or 2 different objects, though they have same name.
when you call func1(nums), mean that you pass a reference. Now the 2 variable nums point to same object. (2 variables, 1 object)
when you assign in func1, the inside variable nums will point to a new object, the outside is still unchanged (2 variables, 2 object)
and when you call print nums then this nums is the outside variable,
There are two reasons for this result:
Variables are simply names that refer to objects
List is mutable
In func1,nums refer to a new object because a new list is created. Therefore global nums is not affected.
In func2, the modification is applied to the object passed in. Thus global nums is changed. A new object is not created because list is mutable.
ref: https://docs.python.org/3/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python
I need to build 3 funcs.
the 1st is insertion sort, 2nd is generate list of random nums between 0-1 and the 3rd need to create list of randon numbers (using 2nd func.) and sort them (using 1st func.).
I can't change the tests in the end and not the arguments of the funcs.
I have a problem with func 3, it says NameError: global name 'my_list' is not defined.
The other funcs works fine, where am I wrong in the 3rd?
*the 1st func not allowed to return anything.
thanks!
my code:
def insertion_sort(lst):
for i in range(1,len(lst)):
current=lst[i]
j=i-1
while j>=0:
if current < lst[j]:
lst[j+1] = lst[j]
lst[j] = current
j-=1
else:
break
def random_list(n):
import random
my_list=[]
for i in range(n):
my_list.append (random.random ())
return my_list
def sorted_random_list(n):
random_list(n)
insertion_sort(my_list)
### TEST FUNCTION - DON'T CHANGE THIS ####
def test_sorted(lst):
print lst == sorted(lst) and len(lst) > 0
def test_len(lst, length):
print len(lst)==length
def sort_and_test(lst):
lst = lst[:]
insertion_sort(lst)
test_sorted(lst)
sort_and_test([13,54,3434,88,334,6,8,84,57,4,2,4,6,6])
sort_and_test(["dog","cat","cow","zebra","frog","bat","spider","monkey"])
sort_and_test(["hi","hello","how are you","what your doing"])
test_len(random_list(10), 10)
lst = sorted_random_list(10)
test_len(lst, 10)
test_sorted(lst)
It's because you call random_list(n) but you don't assign the value it returns to any variable.
Try this instead:
my_list = random_list(n)
And that should solve your problem
Without assigning the return value of random_list() to a variable, all you're doing is computing random_variable() and throwing away the result. What you want to do is to name the result of the call to random_variable() so that you can refer to it later. This is done by assigning it to a variable with that name, in this case, my_list
Hope this helps
Well, the quickest way is to replace the code in sorted_random_list() with
new_list = random_list(n)
insertion_sort(new_list)
return new_list
but the deeper problem here appears to be a lack of understanding of scoping and the need for assignment of values to variables. my_list only exists within the scope of random_list(), so it is not available in the global namespace, which is why you are getting the error you are seeing here. You also aren't actually assigning the result of random_list(n) to anything, so you are throwing away the new list. Then you don't actually return the list created in sorted_random_list() in any case.
my_list is not a global variable. Because you are returning the list from random_list(), in sorted_random_list() you should have my_list = random_list(n). This will create the variable my_list within the scope of that function.