Python Forum
How to use a tuple as an argument of a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use a tuple as an argument of a function
#2
This is not using tuple as argument, but pass arbitrary number of arguments. If you pass tuple as argument it will be considered one argument, not multiple (unless you unpack it).

def spam(*args):
    print(type(args), args)

spam(1, 2, 3)
spam('a', 'b')
Output:
<class 'tuple'> (1, 2, 3) <class 'tuple'> ('a', 'b')
You can also have keyword arguments, so, you can do

def spam(*args, **kwargs):
    print(type(args), args)
    print(type(kwargs), kwargs)


spam(1, 2, 3, foo='eggs', bar='spam')
spam('a', 'b')
Output:
<class 'tuple'> (1, 2, 3) <class 'dict'> {'foo': 'eggs', 'bar': 'spam'} <class 'tuple'> ('a', 'b') <class 'dict'> {}
Note that args and kwargs are names by convention used in such case. I would advise to stick to these.

Here is example to show the difference when you pass a tuple and when you unpack tuple into multiple arguments

def spam(*args, **kwargs):
    print(type(args), args)
    print(f'Number of positional arguments: {len(args)}')
    print(type(kwargs), kwargs)

some_tuple = (1, 2, 3)
spam(some_tuple, foo='eggs', bar='spam')
spam(*some_tuple, foo='eggs', bar='spam')
Output:
<class 'tuple'> ((1, 2, 3),) Number of positional arguments: 1 <class 'dict'> {'foo': 'eggs', 'bar': 'spam'} <class 'tuple'> (1, 2, 3) Number of positional arguments: 3 <class 'dict'> {'foo': 'eggs', 'bar': 'spam'}
zarox likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: How to use a tuple as an argument of a function - by buran - Nov-14-2020, 04:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  mutable argument in function definition akbarza 1 507 Dec-15-2023, 02:00 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,125 Dec-25-2022, 03:00 PM
Last Post: askfriends
  i want to use type= as a function/method keyword argument Skaperen 9 1,929 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  Regex - Pass Flags as a function argument? muzikman 6 3,638 Sep-06-2021, 03:43 PM
Last Post: muzikman
  Tuple generator, and function/class syntax quazirfan 3 3,929 Aug-10-2021, 09:32 AM
Last Post: buran
  Sort Function: <' not supported between instances of 'float' and 'tuple' quest 2 8,114 Apr-30-2021, 07:37 PM
Last Post: quest
  How to check if my argument is a tuple with 3 tuples zarox 1 1,845 Nov-15-2020, 06:50 PM
Last Post: DougBTX
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,864 Nov-04-2020, 11:26 AM
Last Post: Aggam
  calling a function and argument in an input phillup7 3 2,631 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  Passing argument from top-level function to embedded function JaneTan 2 2,283 Oct-15-2020, 03:50 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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