Skip to content

Setting *args as the 1st or middle parameter and passing more arguments than parameters should make *args get the rest of arguments #137788

@hyperkai

Description

@hyperkai

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

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions