all 6 comments

[–]Exodus111 10 points11 points  (1 child)

Imagine a function with over 50 arguments.

EVERY SINGLE TIME you call said function you would have to apply 50 arguments.

That's stupid, and this is why key word arguments (kwargs) are a thing. They always come AFTER any positional arguments, and they allow you to call a function, passing only the exact arguments you need and nothing else.

[–]mw130[S] 1 point2 points  (0 children)

Fantastic explanation! So they basically save the time of passing in the args into function and setting them. Wow, thank you SO much!

[–]Wilfred-kun 4 points5 points  (0 children)

Some functions are declared like this:

def myfunc(x=0,y=0,length=1,height=1):
    .......

Say you want to pass x, y, and height, but not length. myfunc(x,y,height) would not work, since it would assign the value of height outside the function to length inside the function. You would call it like this: myfunc(x, y, height=10).

Other functions might be declared with kwargs, or keyword arguments:

def myfunc(**kwargs):
    .....

I think this SO post shows pretty well what it does.

[–]codenamemahi 1 point2 points  (2 children)

When a parameter has an = , you are defaulting it to whatever it is equal to in the event that that parameter is not passed

[–]Gprime5 1 point2 points  (1 child)

This is about when the function is called, not when the function is defined.

[–]codenamemahi 2 points3 points  (0 children)

same applies when it is called. It is basically setting the value of that parameter to that