How to use a tuple as an argument of a function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to use a tuple as an argument of a function (/thread-30943.html) |
How to use a tuple as an argument of a function - zarox - Nov-14-2020 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? RE: How to use a tuple as an argument of a function - buran - Nov-14-2020 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') You can also have keyword arguments, so, you can dodef spam(*args, **kwargs): print(type(args), args) print(type(kwargs), kwargs) spam(1, 2, 3, foo='eggs', bar='spam') spam('a', 'b') 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')
RE: How to use a tuple as an argument of a function - zarox - Nov-14-2020 That's exactly what I was looking for! Yea I wanted to pass only one argument which was the tuple . Thank you so much!! RE: How to use a tuple as an argument of a function - buran - Nov-14-2020 (Nov-14-2020, 04:42 PM)zarox Wrote: Yea I wanted to pass only one argument which was the tupleJust 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) RE: How to use a tuple as an argument of a function - zarox - Nov-14-2020 (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 tupleJust to make sure we are on the same page Is this method as "correct" as *args and *kwargs? RE: How to use a tuple as an argument of a function - buran - Nov-14-2020 (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) *args, **kwargs is often used in more complex cases, e.g. OOP with class inheritance
|