[Tutor] Defining variable arguments in a function in python
Alan Gauld
alan.gauld at yahoo.co.uk
Sat Dec 29 04:41:17 EST 2018
On 29/12/2018 06:12, Karthik Bhat wrote:
> def fun_varargs(a=5, *numbers, **dict):
> print("Value of a is",a)
>
> for i in numbers:
> print("Value of i is",i)
>
> for i, j in dict.items():
> print("The value of i and j are:",i,j)
>
> fun_varargs(1,2,3,4,5,6,7,8,9,10,Jack=111,John=222,Jimmy=333)
>
> How do I make the tuple 'number' contain the first element to be 1 and not
> 2?
You need to provide a value for a.
The default 5 will only be used if the function is called
without *any* arguments. Otherwise it will always take
the first argument value. So, if you want a to be 5 and
then provide a tuple etc you must explicitly pass a 5 in:
fun_varargs(5, 1,2,3,4,5,6,7,8,9,10, Jack=111,John=222,Jimmy=333)
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list