Why can't you use varargs and keyword arguments together?

S

Sandra-24

I've always wondered why I can't do:

def foo(a,b,c):
return a,b,c

args = range(2)
foo(*args, c = 2)

When you can do:

foo(*args, **{'c':2})

Whenever I stub my toe on this one, I always just use the second
approach, which seems less readable. As with most things in Python,
I've suspected there's a good reason for it. Having just bumped into
this one again, I thought I'd ask if anyone knows why the first syntax
should not be allowed.

This comes up anyplace you need variable arguments and keyword
arguments together but don't have the keyword arguemnts in a dict. In
this case you are forced to put them in a dict. I don't think anyone
would find that to be more readable.

Thanks and Merry Christmas,
-Sandra
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top