Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name of and reason for Python function parameters of type `name=value`

It's entirely possible that this question is a duplicate, but I don't know what this concept is called so I don't even know how to search for it.

I'm new to Python and trying to understand this function from a Caffe example:

def conv_relu(bottom, ks, nout, stride=1, pad=0, group=1):
    conv = L.Convolution(bottom, kernel_size=ks, stride=stride,
                                num_output=nout, pad=pad, group=group)
    return conv, L.ReLU(conv, in_place=True)

I figured the parameters stride=1, pad=1, etc in the conv_relu function definition are default initial values, but then what do kernel_size=ks, stride=stride, etc in the L.Convolution call mean? Is it kind of like a name/value pair?

If nothing else, can someone please tell me what this is called?

like image 581
marcman Avatar asked Mar 19 '16 20:03

marcman


1 Answers

Those are keyword arguments.

some_function(x=y, z=zz) 

x is the name of the parameter when the function was declared, and y is the value that's being passed in.

Reasons to use keyword arguments:

  • You can give them in any order, instead of only the order in the function definition
  • When you look back on the code later, if the parameters have good names, you can immediately tell the purpose of the passed variables instead of needing to check the function definition or documentation to see what the data means.
like image 100
Carcigenicate Avatar answered Sep 28 '22 14:09

Carcigenicate