0

Python: Ordering arguments

a,b,*c,d = 1,2,3 >>> c [] >>> d 3 If formal positional arguments should precede *args (as many tutorials state) why does the code above generates the value of 3 for positional argument “c”?

6th Mar 2020, 4:13 PM
Prof. Dr. Zoltán Vass
2 Antworten
+ 3
>>> a,b,*c,d = 1,2,3 >>> a,b,c,d -> (1, 2, [], 3) >>> a,b,*c,d = 1,2,3,4 >>> a,b,c,d -> (1, 2, [3], 4) >>> a,b,*c,d = 1,2,3,4,5,6,7,8 >>> a,b,c,d -> (1, 2, [3, 4, 5, 6, 7], 8) As you can see, first of all the regular vars are filled with parameters, if n of arguments < n of vars. If n of arguments are >= to n of variables, also *c will be filled.
6th Mar 2020, 5:43 PM
Lothar
Lothar - avatar
0
Lothar Thank you, Lothar. Your examples are great. What I don’t understand that also SoloLearn Python course states that *args must come after positional parameters. However, this code still works even *arg is in the first position: >>> *a,b,c,d = 1,2,3 >>> a [] >>> b 1 >>> c 2 >>> d 3 So order is not important at all?
6th Mar 2020, 6:21 PM
Prof. Dr. Zoltán Vass