Default Function Arguments
Files associated with this lesson:
Advanced Function Arguments.ipynb
Advanced Function Arguments¶
Named Arguments¶
Do you see a problem with the following function:
compute_profit(30000, 1500, 7500, 910)
It's not readable. You don't know what each value represents. A little bit better would be:
compute_profit(sales=30000, refunds=1500, costs=7500, expenses=910)
It's the same thing, but we're explicitly including the parameter.
In [1]:
def say_name(name, times):
for time in range(times):
print(name)
In [2]:
say_name('John', 3)
In [ ]:
say_name('John', times=3)
In [3]:
say_name(name='John', times=3)
In [4]:
say_name(times=3, name='John')
NOT RECOMMENDED
Default arguments¶
In [5]:
def say_name(name, times=3):
for time in range(times):
print(name)
In [10]:
say_name(name='Mary')
In [ ]: