I'm researching the effect of using *args in between a python function but I don't understand if the use case is practical or even possible, it is marked as error on my IDE.
def my_function(a, *args, b):
print(a)
print(args)
print(b)
my_function(1, 2, 3, 4, 5)
My output is the following:
Traceback (most recent call last):
File "C:/Users/axel_/PycharmProjects/Python_Subject_Exam/3_new_exam_args_in_middle.py", line 10, in <module>
my_function(1, 2, 3, 4, 5)
TypeError: my_function() missing 1 required keyword-only argument: 'b'
So, the *args must be ever at the end of parameters of any function, puting it at the midle is invalid python code right?
I also tested it at the end as intended:
def my_function(a, b, *args):
print(a)
print(args)
print(b)
my_function(1, 2, 3, 4, 5)
Output:
1
(3, 4, 5)
2
Process finished with exit code 0
This pattern is used to force users to specify all parameters after *args
, in your case you must set b
. This would be accepted:
my_function(1, 2, 3, 4, 5, b=6)