0

*args is used to pass variable number of arguments that is when the number of arguments not known beforehand. But how it is working when it is passed as a parameter to a function?

In the code how arg1 identifies the first element of the tuple and not the second one and in the similar way arg2 points to the second element of the tuple not the first or third one .Why?

I saw a plenty of resources in the internet which explains *args and *kwargs but none of them explains how do they work when passed as a parameter.

def some_args(arg_1, arg_2, arg_3):
    print("arg_1:", arg_1)
    print("arg_2:", arg_2)
    print("arg_3:", arg_3)

args = ("Sammy", "Casey", "Alex")
some_args(*args)
dutta.ari
  • 165
  • 4
  • 1
    What do you mean by 'how does it work'? The same way calling `some_args(args[0], args[1], args[2])` works, because the *Python language definition* states how that syntax works and makes it work. – Martijn Pieters Feb 07 '19 at 17:36
  • From a implementation POV, CPython 3.6 and up define a [dedicated `CALL_FUNCTION_EX` opcode](https://docs.python.org/3/library/dis.html#opcode-CALL_FUNCTION_EX) that handles taking values from an iterable to be used as positional arguments. In older Python versions, there are [3 different `CALL_FUNCTION_*` opcodes for the same functionality](https://docs.python.org/3.5/library/dis.html#opcode-CALL_FUNCTION_VAR). – Martijn Pieters Feb 07 '19 at 17:38

0 Answers0