Default Argument Values
The most useful form is to specify a default value for one or more arguments. This creates a function that can be called with fewer arguments than it is defined to allow. For example:
def ask_ok(prompt, retries=4, reminder='Please try again!'):
while True:
ok = input(prompt)
if ok in ('y', 'ye', 'yes'):
return True
if ok in ('n', 'no', 'nop', 'nope'):
return False
retries = retries - 1
if retries < 0:
raise ValueError('invalid user response')
print(reminder)
This function can be called in several ways:
- giving only the mandatory argument:
ask_ok('Do you really want to quit?')
- giving one of the optional arguments:
ask_ok('OK to overwrite the file?', 2)
- or even giving all arguments:
ask_ok('OK to overwrite the file?', 2, 'Come on, only yes orno!')
Comments
Post a Comment