1

I'm learning Python and playing around with function arguments. Just created a test function with following code:

def packer(name = 'Alpha', **kwargs):
    return(kwargs)

And if I call this function as:

dummy_packer = packer(name = 'Bravo', age = 65, beard = False)

The result is:

{'age': 65, 'beard': False}

The variable dummy_packer will not have name value at all in the result. I understand it ignored it because I have defined already at the stage of creating the function. But then why it didn't give me the default value also? Where the name argument is stored?

Thanks

3
  • 1
    The name argument is stored in the name variable. **kwargs doesn't mean "all arguments passed by keyword", it means "all arguments passed by keyword that aren't named in the function signature". Commented Jul 18, 2017 at 19:20
  • See stackoverflow.com/questions/3394835/args-and-kwargs Commented Jul 18, 2017 at 19:21
  • Possible duplicate of *args and **kwargs? Commented Jul 18, 2017 at 19:21

2 Answers 2

2

name is available to you in the function context.

def packer(name = 'Alpha', **kwargs):
    print('name is %s' % (name,))
    return(kwargs)
Sign up to request clarification or add additional context in comments.

2 Comments

might be worth adding that return dict(name=name, **kwargs) could be used to return all arguments
Thanks, both answers helped me.
0

kwargs refers to variable number of keyword arguments. In this case, because name is a defined variable, it is not included in kwargs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.