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
#1
I'm new to python and programming in general. I want to know how to use a tuple as the argument of a function

I've been using

def function(a, b ,c):
    tup = (a, b, c)
but this limits the tuple elements the amount of arguments I put in the function definition.

I'd like to do something like def function(t), where t is a tuple and I can call function(1,2,3) for example. Is this possible?
Reply
#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
#3
That's exactly what I was looking for! Yea I wanted to pass only one argument which was the tuple Big Grin . Thank you so much!!
Reply
#4
(Nov-14-2020, 04:42 PM)zarox Wrote: Yea I wanted to pass only one argument which was the tuple
Just to make sure we are on the same page
if you just want to pass one tuple as argument you don't need this. Just do:
def spam(foo):
    print(type(foo), foo)


eggs = (1, 2, 3)
spam(eggs)
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
#5
(Nov-14-2020, 04:46 PM)buran Wrote:
(Nov-14-2020, 04:42 PM)zarox Wrote: Yea I wanted to pass only one argument which was the tuple
Just to make sure we are on the same page
if you just want to pass one tuple as argument you don't need this. Just do:
def spam(foo):
    print(type(foo), foo)


eggs = (1, 2, 3)
spam(eggs)

Is this method as "correct" as *args and *kwargs?
Reply
#6
(Nov-14-2020, 07:46 PM)zarox Wrote: Is this method as "correct" as *args and *kwargs?

It depend what you want to achieve. This is the correct method if you want to pass one argument (it may be tuple). It will take one and only one argument (it may or may not be the type you expect).

In your initial post you were asking something different, or at least I understood that you ask about arbitrary number of positional arguments - i.e. when you don't know in advance how many arguments you will get or want to allow arbitrary number. In your post:

(Nov-14-2020, 04:21 PM)zarox Wrote: I can call function(1,2,3)
Here you don't pass one tuple, but 3 positional arguments.

def spam(foo):
    print(foo)

foo = (1, 2, 3)
spam(foo)
spam(1, 2, 3)
Output:
(1, 2, 3) Traceback (most recent call last): File "***", line 6, in <module> spam(1, 2, 3) TypeError: spam() takes 1 positional argument but 3 were given
*args, **kwargs is often used in more complex cases, e.g. OOP with class inheritance
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort Function: <' not supported between instances of 'float' and 'tuple' quest 2 361 Apr-30-2021, 07:37 PM
Last Post: quest
  How to check if my argument is a tuple with 3 tuples zarox 1 374 Nov-15-2020, 06:50 PM
Last Post: DougBTX
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 639 Nov-04-2020, 11:26 AM
Last Post: Aggam
  calling a function and argument in an input phillup7 3 609 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  Passing argument from top-level function to embedded function JaneTan 2 489 Oct-15-2020, 03:50 PM
Last Post: deanhystad
  return string from function with one argument jamie_01 2 697 May-28-2020, 11:07 PM
Last Post: menator01
  how to use getter as argument in function nanok66 3 797 May-13-2020, 09:15 AM
Last Post: nanok66
  Cant define turtle color with function argument Wrightys99 2 621 Apr-22-2020, 01:43 PM
Last Post: Wrightys99
  When Defining a Function with an Equation as a Default Argument, which Value Is Used? OJGeorge4 4 800 Apr-09-2020, 08:48 AM
Last Post: DeaD_EyE
  SyntaxError: positional argument follows keyword argument syd_jat 3 2,855 Mar-03-2020, 08:34 AM
Last Post: buran

Forum Jump:

User Panel Messages

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