+ 1

*Args and default values

Is it possible to call a function that has a default value and an *args argument (not **kwargs), without setting the default value again? e.g. def my_func(a, b=10, *args): print(a, b, args) so b should be 10, and has not been defined when calling the function; or is there a way to define it so that you do not use the optional parameter?

23rd Jan 2022, 11:15 AM
Kamil Hamid
Kamil Hamid - avatar
2 Antworten
+ 2
How about making 'b' a keyword argument: def your_func(a, *args, b=10): print(a, b, args) Notice 'b' after '*args' E.g.: your_func(1) -> 1 10 () your_func(1, 2, 3, 4) -> 1 10 (2, 3, 4) your_func(1, b=8) -> 1 8 () your_func(1, 2, 3, b=8) -> 1 8 (2, 3)
23rd Jan 2022, 1:27 PM
Angelo
Angelo - avatar
0
Ah. Thank you!
23rd Jan 2022, 3:44 PM
Kamil Hamid
Kamil Hamid - avatar