python学习旅程笔记4-函数

函数

一.函数简介

函数是一个程序的重要组成部分。它们允许你给一块语句一个名称,然后你可以在你的程序的任何地方使用这个名称任意多次地运行这个语句块。

在Python中,

函数通过def关键字定义。def关键字后跟一个函数的标示符名称,然后跟一对圆括号。圆括号之中可以包括一些变量名,该行以冒号结尾。接下来是一块语句,它们是函数体。下面这个例子将说明这事实上是十分简单的:

[python]  view plain copy
  1. #!/usr/bin/python  
  2. # Filename: function1.py  
  3.   
  4. def sayHello():  
  5.     print 'Hello World!' # block belonging to the function  
  6.   
  7. sayHello() # call the function   

 

 

二.形参

在函数使用过程中,通过形参和实参的值得传递来完成。

例如:

[python]  view plain copy
  1. #!/usr/bin/python  
  2. # Filename: func_param.py  
  3.   
  4. def printMax(a, b):  
  5.     if a > b:  
  6.         print a, 'is maximum'  
  7.     else:  
  8.         print b, 'is maximum'  
  9.   
  10. printMax(34# directly give literal values  
  11.   
  12. x = 5  
  13. y = 7  
  14.   
  15. printMax(x, y) # give variables as arguments   

 

输出为:

 

[python]  view plain copy
  1. $ python func_param.py  
  2. 4 is maximum  
  3. 7 is maximum   

 

 

三.局部变量

你在函数定义内声明变量的时候,它们与函数外具有相同名称的其他变量没有任何关系,即变量名称对于函数来说是局部的。这称为变量的作用域。所有变量的作用域是它们被定义的块,从它们的名称被定义的那点开始。

[python]  view plain copy
  1. #!/usr/bin/python  
  2. # Filename: func_local.py  
  3.   
  4. def func(x):  
  5.     print 'x is', x  
  6.     x = 2  
  7.     print 'Changed local x to', x  
  8.   
  9. x = 50  
  10. func(x)  
  11. print 'x is still', x   

 

输出为:

[python]  view plain copy
  1. $ python func_local.py  
  2. is 50  
  3. Changed local x to 2  
  4. is still 50   

 

关于global语句

如果你想要为一个定义在函数外的变量赋值,那么你就得告诉Python这个变量名不是局部的,而是全局 的。

你可以使用定义在函数外的变量的值(假设在函数内没有同名的变量)。但是,这样使用的话,不利于代码的可读性。

[python]  view plain copy
  1. #!/usr/bin/python  
  2. # Filename: func_global.py  
  3.   
  4. def func():  
  5.     global x  
  6.   
  7.     print 'x is', x  
  8.     x = 2  
  9.     print 'Changed local x to', x  
  10.   
  11. x = 50  
  12. func()  
  13. print 'Value of x is', x   

 

输出为:

 

[python]  view plain copy
  1. $ python func_global.py  
  2. is 50  
  3. Changed global x to 2  
  4. Value of x is 2   

 

注意:你可以使用同一个global语句指定多个全局变量。例如global x, y, z

四.默认参数值

对于一些函数,你可能希望它的一些参数是可选的,如果用户不想要为这些参数提供值的话,这些参数就使用默认值。这个功能借助于默认参数值完成。你可以在函数定义的形参名后加上赋值运算符(=)和默认值,从而给形参指定默认参数值。

例如:

[python]  view plain copy
  1. #!/usr/bin/python  
  2. # Filename: func_default.py  
  3.   
  4. def say(message, times = 1):  
  5.     print message * times  
  6.   
  7. say('Hello')  
  8. say('World'5)   

 

输出为:

 

[python]  view plain copy
  1. $ python func_default.py  
  2. Hello  
  3. WorldWorldWorldWorldWorld   

 

注意:只有在形参表末尾的那些参数可以有默认参数值,即你不能在声明函数形参的时候,先声明有默认值的形参而后声明没有默认值的形参。
这是因为赋给形参的值是根据位置而赋值的。例如,def func(a, b=5)是有效的,但是def func(a=5, b)是无效的。

 

五.关键参数

如果你的某个函数有许多参数,而你只想指定其中的一部分,那么你可以通过命名来为这些参数赋值——这被称作关键参数,我们使用名字(关键字)而不是位置(我们前面所一直使用的方法)来给函数指定实参。

这样做有两个优势,由于我们不必担心参数的顺序,使用函数变得更加简单了。二、假设其他参数都有默认值,我们可以只给我们想要的那些参数赋值。

例如:

[python]  view plain copy
  1. #!/usr/bin/python  
  2. # Filename: func_key.py  
  3.   
  4. def func(a, b=5, c=10):  
  5.     print 'a is', a, 'and b is', b, 'and c is', c  
  6.   
  7. func(37)  
  8. func(25, c=24)  
  9. func(c=50, a=100)   

 

输出为:

 

[python]  view plain copy
  1. $ python func_key.py  
  2. is 3 and b is 7 and c is 10  
  3. is 25 and b is 5 and c is 24  
  4. is 100 and b is 5 and c is 50   

 

 

六.return语句

[python]  view plain copy
  1. #!/usr/bin/python  
  2. # Filename: func_return.py  
  3.   
  4. def maximum(x, y):  
  5.     if x > y:  
  6.         return x  
  7.     else:  
  8.         return y  
  9.   
  10. print maximum(23)   

 

输出为:

 

[python]  view plain copy
  1. $ python func_return.py  
  2. 3   

 

七.关于文档字符串

Python有一个很奇妙的特性,称为文档字符串,它通常被简称为 docstrings 。DocStrings是一个重要的工具,由于它帮助你的程序文档更加简单易懂,你应该尽量使用它。你甚至可以在程序运行的时候,从函数恢复文档字符串.

[python]  view plain copy
  1. #!/usr/bin/python  
  2. # Filename: func_doc.py  
  3.   
  4. def printMax(x, y):  
  5.     '''''Prints the maximum of two numbers. 
  6.  
  7.     The two values must be integers.'''  
  8.     x = int(x) # convert to integers, if possible  
  9.     y = int(y)  
  10.   
  11.     if x > y:  
  12.         print x, 'is maximum'  
  13.     else:  
  14.         print y, 'is maximum'  
  15.   
  16. printMax(35)  
  17. print printMax.__doc__   

 

输出为:

 

[python]  view plain copy
  1. $ python func_doc.py  
  2. 5 is maximum  
  3. Prints the maximum of two numbers.  
  4.   
  5.         The two values must be integers.   

 

在函数的第一个逻辑行的字符串是这个函数的文档字符串。注意,DocStrings也适用于模块和类。

 

文档字符串的惯例是一个多行字符串,它的首行以大写字母开始,句号结尾。第二行是空行,从第三行开始是详细的描述。使用文档字符串时遵循这个惯例。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值