Any answer here is subjective but I'm not going to submit to close the question because I think this is valuable for anyone learning Python and professional practice.
With that said, I think the way you have it is correct, if b
in foo
is assumed to be used in a line of code within the function after bar
is called. If b
isn't used, you should remove it from the parameter list.
But let me make another case while we're at subjectivity: specify arguments upon a function call by use of keyword arguments as much as possible.
Why?
Because when you're looking at complex code in a professional environment, it becomes much more clear what's being passed into a function and why. And in Python, explicit is preferred over implicit.
You will write your code once. You might correct it a couple of times. Someone will read your code to handle a production issue a thousand times.
Take for example the below code (which is a financial model):
def black_scholes(spot, strike, volatility, risk_free_rate, expiry=.25):
# do calculation and return result
# ....
return result
option_value = black_scholes(44, 48, .08, .54, .75)
Put your hand over the parameter list in the function definition. Can you tell what each of those numbers represents?
Sure, you can find the definition in the same file and compare the positional arguments, but what if black_scholes()
is in another module? Now it's a tiny bit tougher. What if we expanded on this and added a higher-leveled wrapper around black_scholes()
and that's what we had to debug to get to this?
Now, let me show you the function call with keyword arguments:
result = black_scholes(spot=44, strike=48, volatility=.08, risk_free_rate=.54, expiry=.75)
This now becomes much more clear, and we can anticipate what the expected result should be. We also just saved a lot of time on reading the code and have a result to compare with (given the expected) from stepping over the function in the debugger instead of having to go into it and reading it line by line.