返回顶部

收藏

Default Argument Values: a simple demo

更多
# 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, complaint='Yes or no, please!'):
    while True:
        ok = raw_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 IOError, 'refusenik user'
        print complaint

#This function can be called either like this: 

ask_ok('Do you really want to quit?') 

# or like this: 

ask_ok('OK to overwrite the file?', 2) 

标签:Python,Function

收藏

0人收藏

支持

0

反对

0

发表评论