Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

How to: *args, **kwargs, *args

I'm just creating a test function to see how kwargs and args work. What if I want to have a function which uses: string, *args, **kwargs, **args2

I was using this simple test function which works:

def test(required, *args, *kwargs,): print(required) if args: print(args) if kwargs: print(kwargs)

test('Hello', 1, 2, 3, key1='value', key2=999)

If I want to add a secondary args to this....is it possible? Example adds *args2 after *kwargs.

def test(required, *args, *kwargs, *args2): print(required) if args: print(args) if kwargs: print(kwargs) if args2: print(args2)

test('Hello', 1, 2, 3, key1='value', key2=999, ??????)

I've tried calling it, *args2=xx, I've tried just adding numbers in the same format as args 1 after the kwargs.....but can't figure it out.

1 Answer

You say:

What if I want to have a function which uses: string, *args, **kwargs, **args2

I don't believe that is allowed by python. You can have *args and **kwargs, but nothing else can come after that. args will grab any unnamed arguments that weren't already listed in the function definition. kwargs will grab any keyword arguments that aren't listed earlier in the function definition. There would be nothing left over to go into args2.

Does that help?