Python Forum
Why args type is always tuple, when passed it as argument to the function.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why args type is always tuple, when passed it as argument to the function.
#1
I am passing list argument to the function. But type of args is always tuple.
I would like to know the specific reason for this behavior?
>>> def fun(*args):
...     print "type of args :", type(args)
... 
>>> l=[1,2,3,4]
>>> fun(l)
type of args : <type 'tuple'>
Reply
#2
It is because of the * that you wrote in fun(*args). This * is used when one writes a variadic function that takes a variable number of arguments. If you want to pass a single list argument, use def fun(args).
Reply
#3
Hi Gribouillis,
Thanks for the replay.
Still my doubt is not cleared. why only tuple is created because of * in fun(*args), why can't list?

Regards,
Praveen.
Reply
#4
It is the way variadic functions work in python. If you define fun(*args), then you can call
fun(7,8,9) and you have args = (7, 8, 9), a tuple with 3 items. If you call
fun(7), you have args = (7,) a tuple with a single item. Finally, if you call fun([ 3, 4, 5]), then args = ([3, 4, 5],), a tuple which single item is a list.
Reply
#5
https://docs.python.org/3.6/tutorial/con...ment-lists
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
Hi Gribouillis and Wavic,

Thanks for the reply and got clarified.

Regards,
Praveen
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 279 Feb-17-2024, 12:29 PM
Last Post: deanhystad
  mutable argument in function definition akbarza 1 426 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  determine parameter type in definition function akbarza 1 550 Aug-24-2023, 01:46 PM
Last Post: deanhystad
Question How to compare two parameters in a function that has *args? Milan 4 1,198 Mar-26-2023, 07:43 PM
Last Post: Milan
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,033 Dec-25-2022, 03:00 PM
Last Post: askfriends
  i want to use type= as a function/method keyword argument Skaperen 9 1,777 Nov-06-2022, 04:28 AM
Last Post: Skaperen
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 5,975 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  search a list or tuple for a specific type ot class Skaperen 8 1,855 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  match type with value in csv parsing function vinci 2 1,269 Jan-21-2022, 12:19 PM
Last Post: Larz60+
  detecting a generstor passed to a funtion Skaperen 9 3,468 Sep-23-2021, 01:29 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020