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

Hannan
  • 1,171
  • 6
  • 20
  • 39
  • 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". – BrenBarn Jul 18 '17 at 19:20
  • See https://stackoverflow.com/questions/3394835/args-and-kwargs – akshaynagpal Jul 18 '17 at 19:21
  • Possible duplicate of [\*args and \*\*kwargs?](https://stackoverflow.com/questions/3394835/args-and-kwargs) – akshaynagpal Jul 18 '17 at 19:21

2 Answers2

2

name is available to you in the function context.

def packer(name = 'Alpha', **kwargs):
    print('name is %s' % (name,))
    return(kwargs)
kichik
  • 33,220
  • 7
  • 94
  • 114
0

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

Cowabunghole
  • 191
  • 1
  • 8