This question already has answers here:
Python for loop returns True after first item
(4 answers)
Closed 3 years ago.
I'm trying to make a function using *args. It should take in certain numbers and then output the products of the numbers multiplied by 10.
I'm learning to use *args but I'm having trouble getting the output. My result only outputs the first number in the function. I've tried using a for loop, thinking it would iterate over each character, but it didn't work.
def myfunc(*args):
for args1 in args:
return args1*10
When I run the function:
myfunc(10.1,20)
The expected output should be
(101.0,200.0)
The actual output is
(101.0)
Any help would be appreciated. Thanks.
If I understand you, that's what you need:
def myfunc(*args):
return tuple(map(lambda x: x*10, args))
*args can be retrieved in the function as a list called args. What you do here is just multiplying the first element by 10 and return it, so the function stops.
You should use yield instead of return, to make your function return a generator, which can then be converted to tuple whenever you need
>>> def myfunc(*args):
... for args1 in args:
... yield args1*10
...
>>>
>>> myfunc(10.1,20)
<generator object myfunc at 0x7f20d1ae8ad0>
>>>
>>> tuple(myfunc(10.1,20))
(101.0, 200)
Related
This question already has answers here:
What does the "at" (#) symbol do in Python?
(14 answers)
How do I make function decorators and chain them together?
(20 answers)
Closed 26 days ago.
#the code given is:
def f(a):
print('hello')
#f
def f(a,b):
return a%b
f(4,0)
What I expected to happen was a zero division error, instead it printed hello.
When I write the same code without '#f'
it gives the expected result as output
I've never seen symbol/expression being used in python
This is new and Google too has nothing about it
Decorator syntax is a shorthand for a particular function application.
#f
def f(a, b):
return a%b
is roughly equivalent to
def tmp(a, b):
return a % b
tmp2 = f(tmp)
f = tmp2
(Reusing the name f for everything makes this tricker to understand.)
First, your division-by-zero function is defined. Next, your original function f is called: it prints hello and returns None. Finally, you attempt to call None with two arguments, but it isn't callable, resulting in the TypeError you allude to.
In short, the decorator syntax takes care of treating all the things that would otherwise have been named f as distinct objects and applying things in the correct order.
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 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
This question already has answers here:
Pass a list to a function to act as multiple arguments [duplicate]
(3 answers)
Closed 9 years ago.
I am writing a little script in which I call itertools.product like so:
for p in product(list1,list2,list3):
self.createFile(p)
Is there a way for me to call this function without knowing in advance how many lists to include?
Thanks
You can use the star or splat operator (it has a few names): for p in product(*lists) where lists is a tuple or list of things you want to pass.
def func(a,b):
print (a,b)
args=(1,2)
func(*args)
You can do a similar thing when defining a function to allow it to accept a variable number of arguments:
def func2(*args): #unpacking
print(args) #args is a tuple
func2(1,2) #prints (1, 2)
And of course, you can combine the splat operator with the variable number of arguments:
args = (1,2,3)
func2(*args) #prints (1, 2, 3)
Use the splat operator(*) to pass and collect unknown number of arguments positional arguments.
def func(*args):
pass
lis = [1,2,3,4,5]
func(*lis)
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