注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

记录~~~

Stay Hungry. Stay Foolish.

 
 
 

日志

 
 

Python 基础-函数  

2013-06-15 23:08:35|  分类: Python |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
>>> def fib2(n): #return Fibonacci series up to n         #如何从函数中返回一个包含菲波那契数列的数值链表
... """ Return a list containing the Fibonacci series up to n. """
... result = []
... a,b=0,1
... while b<n:
... result.append(b) # see below
... a,b = b,a+b
... return result
... #这里要回车 不要输入
>>> f100 = fib2(100) # call it
>>> f100 # write the result
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>>


4.7 深入函数定义
4.7.1 参数默认值 :

>>> def ask_ok(prompt, retries=4,complaint='Yes or no,please!'):
... while True:
... ok=raw_input(prompt)
... if ok in ('y','ye','yes'): return 1
... if ok in ('n','no','nop','nope'): return 0
... retries = retries-1
... if retries <0: raise IOError,'refusenik user'
... print complaint
...
>>> ask_ok('Do you really want to quit?')
Do you really want to quit?y
1
>>> ask_ok('Do you really want to quit?')
Do you really want to quit?no
0
>>> ask_ok('Do you really want to quit?')
Do you really want to quit?
Yes or no,please!
Do you really want to quit?i
Yes or no,please!
Do you really want to quit?y
1
>>> ask_ok('Do you really want to quit?',1)
Do you really want to quit?d
Yes or no,please!
Do you really want to quit?y
1
>>> ask_ok('Do you really want to quit?',0)
Do you really want to quit?
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in ask_ok
IOError: refusenik user
>>>


>>>
>>> i=5
>>> def f(arg=i):
... print arg
...
>>> i =6
>>> f() #默认值在函数定义段被解析,如下所示:
5
>>>


重要警告:默认值只会解析一次。当默认值是一个可变对象,诸如链表、字典或大部分类实例时,会产生一些差异。例如,以下函数在后继的调用中会积累它的参数值:

>>> def f(a,L=[]):
... L.append(a)
... return L
...
>>> print f(1)
[1]
>>> print f(2)
[1, 2]
>>> print f(3)
[1, 2, 3]
>>> print f(1)
[1, 2, 3, 1]
>>>


如果你不想在不同的函数调用之间共享参数默认值,可以如下面的实例一样编写函数:

>>> def f(a,L=None):

... if L is None:
... L=[]
... L.append(a)
... return L
...
>>> print f(1)
[1]
>>> print f(2)
[2]
>>> print f(3)
[3]
>>>

4.7.2 参数关键字
    #
函数可以通过参数关键字的形式来调用,形如“keyword = value”。例如,以下的函数:

>>> 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)
-- This parrot wouldn't voom if you put 1000 Volts through it.
--Lovely plumage the Norwegian Blue
-- It's a stiff !
>>> parrot(action='V0000M',voltage=1000000)
-- This parrot wouldn't V0000M if you put 1000000 Volts through it.
--Lovely plumage the Norwegian Blue
-- It's a stiff !
>>> parrot('a thousand',state='pushing up the daisies')
-- This parrot wouldn't voom if you put a thousand Volts through it.
--Lovely plumage the Norwegian Blue
-- It's pushing up the daisies !
>>> ##以下是错误的调用

>>> parrot() # required argument missing(缺少必要参数)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: parrot() takes at least 1 argument (0 given)
>>> parrot(voltage=5.0,'dead')
File "<stdin>", line 1
SyntaxError: non-keyword arg after keyword arg
>>> parrot(voltage=5,'dead') # non-keyword argument following keyword(在关键字后面有非关键字参数)
File "<stdin>", line 1
SyntaxError: non-keyword arg after keyword arg
>>> parrot(voltage=5,action='dead')
-- This parrot wouldn't dead if you put 5 Volts through it.
--Lovely plumage the Norwegian Blue
-- It's a stiff !
>>>

>>> parrot(110,voltage=220) # duplicate value for argument(对参数进行了重复赋值)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: parrot() got multiple values for keyword argument 'voltage'
>>> parrot(action =110,voltage=220)
-- This parrot wouldn't 110 if you put 220 Volts through it.
--Lovely plumage the Norwegian Blue
-- It's a stiff !
>>> parrot(actor='John Cleese') # unknown keyword(未知关键字)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: parrot() got an unexpected keyword argument 'actor'
>>> parrot(action='John Cleese')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: parrot() takes at least 1 non-keyword argument (0 given)
>>> parrot(11,action='John Cleese')
-- This parrot wouldn't John Cleese if you put 11 Volts through it.
--Lovely plumage the Norwegian Blue
-- It's a stiff !
>>>


4.7.5 文档字符串

这里介绍文档字符串的概念和格式。

第一行应该是关于对象用途的简介。简短起见,不用明确的陈述对象名或类型,因为它们可以从别的途径了解到(除非这个名字碰巧就是描述这个函数操作的动词)。这一行应该以大写字母开头,以句号结尾。

如果文档字符串有多行,第二行应该空出来,与接下来的详细描述明确分隔。接下来的文档应该有一或多段描述对象的调用约定、边界效应等。

Python的解释器不会从多行的文档字符串中去除缩进,所以必要的时候应当自己清除缩进。这符合通常的习惯。第一行之后的第一个非空行决定了整个文档的缩进格式。(我们不用第一行是因为它通常紧靠着起始的引号,缩进格式显示的不清楚。)留白“相当于”是字符串的起始缩进。每一行都不应该有缩进,如果有缩进的话,所有的留白都应该清除掉。相当于留白就是验证后的制表符扩展(通常是8个空格)。(这一段译得不通,有疑问的读者请参见原文--译者)

以下是一个多行文档字符串的示例:


>>> def my_fun():
... """Do nothing, but document it.
...
... No,really,it doesn't do anything.
... """
... pass
...
>>> print my_fun.__doc__
Do nothing, but document it.

No,really,it doesn't do anything.

>>>


  评论这张
 
阅读(214)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018