How do you write a function in Python with variable arguments into the function and return variable number of outputs? Is this even possible with the constraints?
Asked
Active
Viewed 128 times
-3
-
https://docs.python.org/2/tutorial/controlflow.html#more-on-defining-functions – BrenBarn Feb 16 '15 at 20:41
-
1Please see http://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-python-parameters . – Łukasz Rogalski Feb 16 '15 at 20:42
-
http://stackoverflow.com/questions/919680/python-can-a-variable-number-of-arguments-be-passed-to-a-function – Feb 16 '15 at 20:43
-
@BrenBarn I think you mean [here](https://docs.python.org/2/tutorial/controlflow.html#arbitrary-argument-lists). – Malik Brahimi Feb 16 '15 at 20:50
-
@MalikBrahimi: Yes, but the discussion of `**kwargs` is before that so I decided to link to the top of the section. – BrenBarn Feb 16 '15 at 20:51
2 Answers
2
You mean like this. This is quite similar to ellipses in Java. When supplied with a variable amount of arguments, you can unpack these arguments as a list which you can manipulate as you deem necessary.
def func(*args):
print len(args) # num of vars

Malik Brahimi
- 16,341
- 7
- 39
- 70
0
yes you can, take a look at splat operator
def my_function(*a): #here a will be list or tuple depend wt u passed
# do your stuff with a
**a
for dictionary

Hackaholic
- 19,069
- 5
- 54
- 72
-
-
I think your answer's fine, though it could use some more explanation. In all honesty, this is definitely a duplicate, so I'll upvote to restore justice. – Malik Brahimi Feb 16 '15 at 20:47