This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
I have written the following python program:
#!/usr/bin/env python
def bug( numbers = [] ):
numbers.append( 1 )
return numbers
print bug()
print bug()
The result i would expect is
[1]
[1]
But i got
[1]
[1, 1]
Is this a bug?
No, this is not a bug and this behaviour has been around in Python for a very long time.
The problem is that the list object is mutable, i.e. you can change it, and when you call a function you don't get a new default value. What's happening is this:
def bug( numbers = [] ):
numbers.append( 1 )
return numbers
At this point the function bug has been created and the list that is default value for numbers created.
print bug()
Now we've called bug once and added 1 to the list that was created when the function was defined.
print bug()
When we call the function again we get the same list as before so we get two 1s added to the list.
The usual solution is to define your function as follows:
def bug(numbers = None):
if numbers is None:
numbers = []
numbers.append(1)
return numbers
Read this for more details.
numbers=[] is evaluated only once (when the function is defined). So it's always the same list.
To avoid this, change the function like this:
def not_a_bug(numbers=None):
if numbers is None:
numbers = []
numbers.append(1)
return numbers
Related
This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Closed 13 days ago.
This snippet of python code
def changeValue(x):
x = 2*x
a = [1]
changeValue(a[0])
print(a[0])
Will print the result "1" when I would like it to print the result "2". Can this be done by only altering the function and no other part of the code? I know there are solutions like
def changeValue(x):
x[0] = 2*x[0]
a = [1]
changeValue(a)
or
def changeValue(x):
return 2*x
a = [1]
a[0] = changeValue(a[0])
I am wondering if the argument passed as-is can be treated as a pointer in some sense.
[edit] - Just found a relevant question here so this is likely a duplicate that can be closed.
No it's not possible. If you pass a[0], it's an int and it can't be mutated in any way.
If the int was directly in the global namespace, you could use global keyword. But it's not. So again no.
This question already has answers here:
Return multiple values over time
(2 answers)
Closed 5 years ago.
I've written a function in python to see If i know what I'm doing but unfortunately I'm not. I thought my defined arg variable will hold a number and multiply each of the numbers which *args variable holds and print it all at a time. But, it is giving me the first result only.
This is what I've written:
def res_info(arg,*args):
for var in args:
return var*arg
print(res_info(2,70,60,50))
Having:
140
Expected:
140
120
100
I don't understand why I'm not getting all the results. Thanks for taking a look into it.
You are on the right path. The problem you had was due to your use of the return statement. Use yield instead.
>>> def res_info(arg,*args):
for var in args:
yield var*arg
>>> list(res_info(2,70,60,50))
=> [140, 120, 100]
So, what was happening was, even though your logic was correct, since there was a return statement in the loop, your loop hence was never fully executed and your program scope would come out on the first iteration of the loop itself (ie, when var = 70).
Using yield, solved the problem as it returns a Generator object after all calculations, and hence does not exit the loop like return does.
def res_info(arg,*args):
result = []
for var in args:
result.append(var*arg)
return result
print(res_info(2,70,60,50))
This question already has answers here:
calculate length of list recursively [duplicate]
(2 answers)
Closed 7 years ago.
So, my goal is to count the number of arguments in a list using recursion. However, as I'm only to use one argument in the function call, I don't know how to solve it without a second "count" argument.
So far I have this, where I accumulate the 1's together.
def countElements(a):
if a==[]:
return []
else:
return [1] + countElements(a[1:])
def main():
a=[3,2,5,3]
print(countElements(a))
main()
Instead of returning an empty list in the base case, return 0, and instead of [1] + countElements(a[1:]), return 1 + countElements(a[1:]). That way, you're just keeping the running count instead of getting a list back.
This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Closed 7 years ago.
In detail, my question is this:
Given the following code,
x = 10
def func(x):
x = x+1
def main():
print(x)
func(x)
print(x)
if __name__ == '__main__':
main()
On running this I get:
10
10
Does this mean that Python does not pass values by reference?
And I did check through the other question of the sort, and most(if not all) included analogies of lists or other such examples.
Is it possible to explain this in simple terms, like just a simple integer?
Ps. I am a beginner to coding.
Thanks
If you are coming from a background such as C or C++, which I did, this can be maddening until you figure it out.
Python has names, not variables, and names are bound to objects. Effectively, you can think of all 'variables' or names, as being pointers to python objects.
In python, integers, floats, and strings are immutable. So when you do the following:
x = 10
x = x + 1
You are first binding the name x to the integer 10, then when you evaluate x + 1 you get a new object 11 and then you bind x to that object. Your x inside the function body is local to the function, and when you bind it to 11, the global x remains bound to 10.
If you were to pass a list to the function, and append something to the list, that list would be modified. A list in python is a mutable object. All names bound to the list would refer to the modified list.
As a result, when you pass mutable objects it may seem as if you are passing by reference, and when you pass immutable objects it may seem like you are passing by value.
This question already has an answer here:
Why does the print function return None?
(1 answer)
Closed 5 months ago.
As a very noob with in Python I'm printing all elements of a list in version 3, and after a comprehensive research I couldn't find an explanation for this kind of behavior.
However, I know every function must return some value and when it's not defined the function returns "Null" (or "None" in Python). But why in this case, after printing all elements correctly it prints "None" for each element in another list?
>>> a_list = [1,2]
>>> a_list
[1, 2]
>>> [print(f) for f in a_list]
1
2
[None, None]
None is the return value of the print function.
Don't use [print(f) for f in a_list] when you mean for f in a_list: print(f).
As mentioned by others, print() does not return anything. Hence, None is printed. If you are wondering why the elements are properly printed, and then followed by 2 None's, its because of how functions work.
A function is called and once every statement inside has been executed, a value is returned, but only if the function returns something.
In your case, print(f) was the call to the print function on f, print was executed, meaning that it printed the required value to the console, and then its value was returned, which is None, since print() does n't return anything. As you are working in the shell, each expression is printed directly, and thus, you get both the expected elements along with the None's.
Coming to the solution, you could use a simple loop as mentioned in other answers
The output:
1
2