函数、默认参数、关键字参数

来源:互联网 发布:感恩的现代诗知乎 编辑:程序博客网 时间:2024/06/06 12:53

1、函数定义:

>>> deffibs(n):

...     fib = [0, 1]

...     for i in range(n-2):

...         fib.append(fib[-2] + fib[-1])

...     return fib

...

>>> printfibs(10)

[0, 1, 1, 2, 3, 5,8, 13, 21, 34]


2、默认参数

 

def ask_ok(prompt,retries=4, complaint='Yes or no, please!'):

    while True:

ok= raw_input(prompt)

ifok in ('y', 'ye', 'yes'):

    return True

ifok in ('n', 'no', 'nop', 'nope'):

    return False

retries= retries - 1

ifretries < 0:

    raise IOError('refusenik user')

printcomplaint

 

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

Do you really wantto quit?yes

True

 

 

3、关键字参数

 

def parrot(voltage,state='a stiff', action='voom', type='Norwegian Blue'):

    print "-- This parrot wouldn't",action,

    print "if you put", voltage,"volts through it."

    print "-- Lovely plumage, the",type

    print "-- It's", state,"!"

 

 

parrot(1000)                                                                  # 1个位置参数

>>>kw_argument.parrot(10000)

-- This parrotwouldn't voom if you put 10000 volts through it.

-- Lovely plumage,the Norwegian Blue

-- It's a stiff !

 

parrot(voltage=1000)                                                  # 1个关键字参数

>>>kw_argument.parrot(voltage=10000)

-- This parrotwouldn't voom if you put 10000 volts through it.

-- Lovely plumage,the Norwegian Blue

-- It's a stiff !

 

parrot(voltage=1000000, action='VOOOOOM')     # 2个关键字参数

>>>kw_argument.parrot(voltage=1000000,action='VOOOOOOOM')

-- This parrotwouldn't VOOOOOOOM if you put 1000000 volts through it.

-- Lovely plumage,the Norwegian Blue

-- It's a stiff !

 

parrot(action='VOOOOOM', voltage=1000000)     # 2个关键字参数

>>>kw_argument.parrot(action='VOOOOOOOM',voltage=1000000)

-- This parrotwouldn't VOOOOOOOM if you put 1000000 volts through it.

-- Lovely plumage,the Norwegian Blue

-- It's a stiff !

 

parrot('a million', 'bereft of life', 'jump')                  #3 个位置参数

>>>kw_argument.parrot('a million', 'bereft of life', 'jump')

-- This parrotwouldn't jump if you put a million volts through it.

-- Lovely plumage,the Norwegian Blue

-- It's bereft oflife !

 

parrot('a thousand', state='pushing up the daisies')   # 1个位置参数

>>>kw_argument.parrot('a thousand', state='pushing up the daisies')

-- This parrotwouldn't voom if you put a thousand volts through it.

-- Lovely plumage,the Norwegian Blue

-- It's pushing upthe daisies !


4、收集多余参数

 

ubuntu@ubuntu:~$ catmulti_argument.py

#!/usr/bin/envpython

def cheeseshop(kind,*arguments, **keywords):

    print "-- Do you have any", kind,"?"

    print "-- I'm sorry, we're all outof", kind

    for arg in arguments:

printarg

    print "-" * 40

    keys = sorted(keywords.keys())

    for kw in keys:

printkw, ":", keywords[kw]

 

 

>>>multi_argument.cheeseshop("Limburger", "It's very runny,sir.",

... "It'sreally very, very runny, sir.",

...shopkeeper="Michael Palin",

...client="John Cleese",

...sketch="Cheese Shop Sketch")

-- Do you have anyLimburger ?

-- I'm sorry, we'reall out of Limburger

It's very runny,sir.

It's really very,very runny, sir.

----------------------------------------

client : John Cleese

shopkeeper : MichaelPalin

sketch : Cheese ShopSketch


0 0
原创粉丝点击