函数、默认参数、关键字参数
来源:互联网 发布:感恩的现代诗知乎 编辑:程序博客网 时间: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
- 函数、默认参数、关键字参数
- 【python】函数:函数文档、关键字参数、默认参数、收集参数
- python 函数参数:必选参数、默认参数、可变参数、关键字参数 和 命名关键字参数
- Python函数参数总结(位置参数、默认参数、可变参数、关键字参数和命名关键字参数)
- Python笔记-函数定义、默认参数、关键字参数、参数组合
- python 函数参数(必选参数、默认参数、可选参数、关键字参数)
- python05-函数的位置参数、默认参数、可变参数、键值对参数(关键字参数)
- python函数的参数(默认参数、可变参数、关键字参数和参数组合)
- python 函数参数(必选参数、默认参数、可选参数、关键字参数)
- Python函数参数(默认参数、可变参数、关键字参数及参数组合)
- python 函数的四种参数---必选参数、默认参数、可变参数和关键字参数
- Python 函数的默认参数, 关键字参数的调用
- python--函数(必选参数、默认参数、可变参数、关键字参数)
- python 函数编程的位置参数、默认参数、关键字参数以及函数的递归
- 函数重载 / 默认参数
- 函数的默认参数
- 函数的默认参数
- 函数默认参数
- UML用例图总结
- nginx Thinkphp rewrite
- Kraken项目中如何巧用Q.allSettled装饰nodejs代码的门面
- 触发器和存储过程
- 刷机命令
- 函数、默认参数、关键字参数
- 如何将ios app安装到模拟器
- Android初学习 - Activity 中 onActivityResult() 与 setResult()的调用时机
- Netty-WebSocket长连接推送服务
- php通过循环链解决约瑟夫环
- android防止多次点击
- nginx 访问静态html 方式
- Android 仿微信Activity进入退出动画(右进右出动画 )
- HDU 5057 Argestes and Sequence(平方分割)