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
Question how to type hint a function in a dataclass? Calab 3 990 Feb-27-2025, 04:40 AM
Last Post: Calab
  Extract args=, value that was passed to Multiprocessing.Proc object during runtime? haihal 1 726 Dec-08-2024, 07:04 AM
Last Post: Gribouillis
Question TypeError: argument of type 'NoneType' is not iterable Tajaldeen 7 3,192 Nov-29-2024, 09:45 AM
Last Post: Tajaldeen
  Is changing processes by using match with type function impossible? cametan 1 806 May-30-2024, 02:16 PM
Last Post: cametan
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 1,429 May-13-2024, 05:57 AM
Last Post: Larz60+
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 1,443 Feb-17-2024, 12:29 PM
Last Post: deanhystad
  mutable argument in function definition akbarza 1 1,310 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  determine parameter type in definition function akbarza 1 1,337 Aug-24-2023, 01:46 PM
Last Post: deanhystad
Question How to compare two parameters in a function that has *args? Milan 4 2,998 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 2,498 Dec-25-2022, 03:00 PM
Last Post: askfriends

Forum Jump:

User Panel Messages

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