-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
Closed as not planned
Closed as not planned
Copy link
Labels
type-featureA feature request or enhancementA feature request or enhancement
Description
Feature or enhancement
Proposal:
Setting *args
as the last parameter and passing more arguments than parameters makes *args
get the rest of the arguments (3, 4, 5)
as shown below:
def func(p1, p2, *args):
print(p1, p2, args)
func(1, 2, 3, 4, 5)
# 1 2 (3, 4, 5)
But setting *args
as the 1st or middle parameter and passing more arguments than parameters get error as shown below:
def func(*args, p1, p2):
print(args, p1, p2)
func(1, 2, 3, 4, 5)
# TypeError: func() missing 2 required keyword-only arguments: 'p1' and 'p2'
def func(p1, *args, p2):
print(p1, args, p2)
func(1, 2, 3, 4, 5)
# TypeError: func() missing 1 required keyword-only argument: 'p2'
Actually, setting *variable
as the 1st, midde or last parameter and assigning the more elements of a list than variables makes *variable
get the rest of the elements [1, 2, 3]
[2, 3, 4]
or [3, 4, 5]
as shown below:
*v1, v2, v3 = [1, 2, 3, 4, 5]
print(v1, v2, v3)
# [1, 2, 3] 4 5
v1, *v2, v3 = [1, 2, 3, 4, 5]
print(v1, v2, v3)
# 1 [2, 3, 4] 5
v1, v2, *v3 = [1, 2, 3, 4, 5]
print(v1, v2, v3)
# 1 2 [3, 4, 5]
So, setting *args
as the 1st or middle parameter and passing more arguments than parameters should also make *args
get the rest of the arguments (1, 2, 3)
or (2, 3, 4)
as shown below:
def func(*args, p1, p2):
print(args, p1, p2)
func(1, 2, 3, 4, 5)
# (1, 2, 3) 4 5
def func(p1, *args, p2):
print(p1, args, p2)
func(1, 2, 3, 4, 5)
# 1 (2, 3, 4) 5
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Metadata
Metadata
Assignees
Labels
type-featureA feature request or enhancementA feature request or enhancement