文章目录
1. 什么是函数
在开发程序的过程中,有很多的代码要多次进行使用,为了提高利用率,将独立功能的模块组织为一个小模块
1.1 函数和过程
函数和过程的最大区别就是:函数具有返回值,过程是没有返回值的,在python中过程就是函数,因为解释器会将默认的返回值为None,可以看到下面开发界最出名的hello world,函数体是没有任何的返回值,执行之后就是打印hello world,然后找一个变量接收默认的返回值,打印出可以看出为None
#!/usr/bin/env python
# -*- coding:utf-8 -*-
def hello():
print "hello world"
### 行为为过程,函数体中没有任何的返回值
### 调用函数
hello()
### 查看默认的返回值
res = hello()
print res
输出
hello world
hello world
None
2. 函数
2.1 定义函数
定义函数格式
def 函数名称():
代码块
demo:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
def hello():
print "hello world"
2.2 调用函数
定义函数之后,我们想要使用函数体中的逻辑,需要进行调用,调用方式就是函数名()
hello()
2.3 函数帮助文档
2.4 返回值
函数会想调用者返回一个值,默认的返回值为None,python也可以返回一个值或者对象
返回值个数 | python返回的类型 |
---|---|
0 | None |
1 | object |
> 1 | tuple |
demo
#!/usr/bin/env python
# -*- coding:utf-8 -*-
### 没有返回值
def zero():
print "zero"
### 只有一个返回值
def one():
return "hello world"
### 返回多个返回值
def more():
return 'haozi', 'superman'
print "=" * 3 + "zero" + "=" * 3
print zero()
print "=" * 3 + "one" + "=" * 3
print one()
print "=" * 3 + "more" + "=" * 3
print more()
输出
===zero===
zero
None
===one===
hello world
===more===
('haozi', 'superman')
python 函数return中有多个数值,那么这个数值默认是以元祖的方式进行返回的,也可以将多个返回值进行包装,比如包装成为一个列表,然后返回一个列表,如果有多个值分别返回,这些值要有对应的变量进行接收,否则会因为接收数目报错
damo
#!/usr/bin/env python
# -*- coding:utf-8 -*-
### 返回值为姓名,年龄,角色
def test():
return "zhanghao", "18", "superman"
### 通过name, age, role接收返回值
name, age, role = test()
print name, age, role
输出
zhanghao 18 superman
缺省接收参数
#!/usr/bin/env python
# -*- coding:utf-8 -*-
### 返回值为姓名,年龄,角色
def test():
return "zhanghao", "18", "superman"
### 通过name, age, role接收返回值
name, age = test()
print name, age, role
输出结果: 可以看到直接报错
Traceback (most recent call last):
File "/Users/zhanghao/code/qdata/qdeploy/func.py", line 10, in <module>
name, age = test()
ValueError: too many values to unpack
2.5 函数参数
2.5.1 位置参数
位置参数在调用函数定义要准确的顺序进行传递,并且参数数量要和声明的时候是相同的(默认参数除外),如果调用的顺序想要发生改变,就要将位置参数的关键字进行精确的匹配。
demo
#!/usr/bin/env python
# -*- coding:utf-8 -*-
def getInfo(ip, port):
mathine_ip = ip
mathine_port = port
print "机器IP地址:%s, 机器开放端口:%s" % (mathine_ip, mathine_port)
## 不指定关键字,按照顺序进行输入
getInfo("10.10.88.2", "22")
## 指定关键字,乱序进行输入
getInfo(port="33", ip="10.10.88.3")
输出
机器IP地址:10.10.88.2, 机器开放端口:22
机器IP地址:10.10.88.3, 机器开放端口:33
- 形参
在定义函数的时候,在()用来接收参数的成为形参
- 实参
在调用的时候,在()中传递给函数yoga的成为实参
2.5.2 默认参数
默认参数就是声明了默认值的参数,因为已经有了默认值,所以在调用函数的时候,所以不传入值也是允许的。默认值的声明变量的语法注意是默认参数要在所有位置参数之前。
demo
#!/usr/bin/env python
# -*- coding:utf-8 -*-
def taxMe(cost, rate=0.0825):
return cost + (cost * rate)
print "cost=100,rate为默认值"
print taxMe(100)
print "cost=100, rate为0.05"
print taxMe(100, 0.05)
输出结果
cost=100,rate为默认值
108.25
cost=100, rate为0.05
105.0
2.5.3 可变长的参数
在调用函数的时候,我们每次调用的参数数量是变化的,这个时候需要可变长度的参数列表。函数在调用的时候提供了关键字和非关键字两种参数类型。
- 非关键字可变长参数: 元组
在函数调用的时候,所有形参都赋值给函数声明中对应的局部变量,超出的部分会按照元组的方式,如果没有超出额外的参数,那么元组就为空
demo
#!/usr/bin/env python
# -*- coding:utf-8 -*-
def tupleVarArgs(arg1, arg2='default', *theRest):
'display regular args and non-keywork variable args'
print 'formal arg 1:', arg1
print 'formal arg 2:', arg2
for eachXtrArg in theRest:
print 'another arg:', eachXtrArg
## 传入一个变量
tupleVarArgs('test1')
## 传入多个变量,将默认变量覆盖
tupleVarArgs('test1', 'name', 'age', 'sec')
输出
formal arg 1: test1
formal arg 2: default
formal arg 1: test1
formal arg 2: name
another arg: age
another arg: sec
- 关键字变量参数:字典
在我们有不一定数目或者额外的关键字情况,参数默认放到一个字典中,字典中的键为参数名,为了区分关键字和非关键字的区别,对于非关键字的使用双星号(**)
demo
#!/usr/bin/env python
# -*- coding:utf-8 -*-
def dictVarArgs(arg1, arg2='default', **theRest):
'display 2 regular args and keyword variable args'
print 'formal arg1:', arg1
print 'formal arg2:', arg2
for each in theRest.keys():
print 'Xtra arg %s: %s' % (each, str(theRest[each]))
dictVarArgs('test1', 'test2', name='zhanghao', age=26)
输出结果
formal arg1: test1
formal arg2: test2
Xtra arg age: 26
Xtra arg name: zhanghao