You are on page 1of 3

While making an app in python I encountered “‘user’ is invalid keyword argument for this

function”. I have still problems understanding basic python language-excuse me what is that
again? But in Amharic it’s it just ‘'e?’

Brett Slatkin explains what keyword arguments are in the book - Effective Python. Arguments are often
given at function call. In the following code, the arguments are 20 and 7.

def remainder(number, divisor):


return number % divisor
assert remainder(20, 7) == 6

The arguments 20 and 7 are positional arguments in the above code, because 20 is in the
position of number and 7 is in the position of divisor. All positional arguments to Python
functions can also be passed by keyword, where the name of the argument is used in an
assignment within the parentheses of a function call.

The keyword arguments can be passed in any order as long as all of the required positional
arguments are specified. You can mix and match keyword and positional arguments. When
calling a function, positional arguments must be specified before keyword arguments. These
calls are equivalent:

remainder(20, 7)
remainder(20, divisor=7)
remainder(number=20, divisor=7)
remainder(divisor=7, number=20)

remainder(number=20, 7)
>>>
SyntaxError: non-keyword arg after keyword arg

Each argument can only be specified once.

remainder(20, number=7)
>>>
TypeError: remainder() got multiple values for argument ‘number’

Keyword arguments provide three significant benefits.

First, keyword arguments make the function call clearer to new readers of the code. With the
call remainder(20,7), it’s not evident which argument is the number and which is the
divisor without looking at the implementation of the remainder method. In the call with
keyword arguments, number=20 and divisor=7 make it immediately obvious which parameter is
being used for each purpose. Arguments are supplied during the function call.
Second, while using keyword argument, default values can be specified in the function
definition. This allows a function to provide additional capabilities when you need them but lets
you accept the default behavior most of the time. This can eliminate repetitive code and reduce
noise.
For example, let’s say we want to compute flow rate for water.

def flow_rate(volume_diff, time_diff):


return volume_diff / time_diff
volume_diff = 0.5
time_diff = 3
flow = flow_rate(volume_diff, time_diff)
print('%.5f m3 per second.' % flow)

In the typical case, It’s useful to know the flow rate in cubic meter per second. Other times, we
would want to know the rate in hours or days. You can provide this behavior in the same
function by adding an argument for the time-period scaling factor. The problem is that now you
need to specify the period argument every time you call the function, even in the common case
of flow rate per second (where the period is 1).

def flow_rate(volume_diff, time_diff, period):


return (volume_diff / time_diff) * period
flow_per_second = flow_rate(volume_diff, time_diff, 1)

To make this less noisy, we can give the period argument a default value. Now, the period
argument becomes optional at function call and we may not specify it. This way we could use
different periods- second, hour or days as default.

def flow_rate(volume_diff, time_diff, period=1):


return (volume_diff / time_diff) * period

Code:

def flow_rate(volume_diff, time_diff):


return volume_diff / time_diff

volume_diff = 0.5
time_diff = 3
flow = flow_rate(volume_diff, time_diff)
print("Water flow rate is %.5f m3 per second." % flow)

# Adding period as positional argument. In this case, we need to specify the


argument(value) everytime we call the function
def flow_rate(volume_diff, time_diff, period):
return (volume_diff / time_diff) * period

flow_per_second = flow_rate(volume_diff, time_diff, 1)


print("Water flow rate is %.3f m3 per second." % flow_per_second)
# Less noisy version would be to make period as keyword argument at function
definition(def).period is now optional at function call
def flow_rate(volume_diff, time_diff, period=1):
return (volume_diff / time_diff) * period
flow_per_second = flow_rate(volume_diff, time_diff)
flow_per_hour = flow_rate(volume_diff, time_diff, period=3600)
print("Water flow rate is %.3f m3 per hour." % flow_per_hour)

You might also like