python笔记之函数

函数

>>> def funTest(param1):
...     'this is function document string.' #正文需要有缩进
...     return param1
... 
>>> funTest(12)
12
>>> funTest.__doc__    #访问文档字符串
'this is function document string.'

函数实参,相当于一个赋值操作,为形参赋值

>>> def fuc1(dict):
...     dict['one'] = 1  #函数1修改参数内容
... 
>>> def fuc2(dict):
...     dict = {}  #函数2为参数重新赋值
... 
>>> d = {'one':11, 'two':22}
>>> fuc1(d)   #形参dict赋值为d,即dict和d指向同一地址,修改dict的数据,自然就修改了d的数据
>>> d
{'two': 22, 'one': 1}
>>> fuc2(d) #dict和d指向同一地址,但是重新为dict赋值,则dict与d不再指向同一地址,所以d不变。
>>> d
{'two': 22, 'one': 1}

>>> dd = {'hello':'world'}
>>> fuc1(dd.copy())  #传入dd的一个副本,即dict与dd不指向同一地址,所以dd也不变
>>> dd
{'hello': 'world'}

关键字参数:调用时,使用参数名提供的参数。
定义时使用关键字参数,还可以提供默认值.
参数在给定默认值的时候是可选的。

#打印长方形的名字和宽高
>>> def descRect(name, width, height):
...     print 'name:%s, %d, height:%d' % (name, width, height)
... 
>>> descRect('test', 11, 22)
name:test, 11, height:22
>>> descRect(width=1, height=2, name='test') #定义为位置参数,使用关键字参数调用,与书写顺序无关
name:test, 1, height:2

>>> def descRectangle(name, width=1, height=1):
...     print 'name:%s, %d, height:%d' % (name, width, height) #name是位置参数,width和height是关键字参数,且提供了默认值
... 
>>> descRectangle('small', 2, 3)  #参数齐全
name:small, 2, height:3
>>> descRectangle('default')  #位置参数必需,关键字参数有默认值
name:default, 1, height:1
>>> descRectangle('default', 2)  #没有指明关键字参数名字的,按照顺序
name:default, 2, height:1
>>> descRectangle('default', height=2)  #指明了关键字参数名字的,替换默认值
name:default, 1, height:2
>>> descRectangle(name='test')
name:test, 1, height:1

收集参数
参数名之前加一个星号,表示收集其他的位置参数。
参数名之前加两个星号,表示收集其他的关键字参数。

>>> def chooseOne(myChoose, *others):
...     print 'I choose %s' % myChoose
...     print 'others are ', others
... 
>>> chooseOne('A', 'B', 'C', 'D', 'E')
I choose A
others are  ('B', 'C', 'D', 'E')
>>> chooseOne('A', 'B')
I choose A
others are  ('B',)
>>> chooseOne('A')
I choose A
others are  ()

>>> def printParams(**param):
...     print param
... 
>>> printParams(x=1,y=2,z=3)
{'y': 2, 'x': 1, 'z': 3}
>>> printParams(1,y=2,z=3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: printParams() takes exactly 0 arguments (3 given)
>>> 

>>> def print_params(x,y,z=0):
...     print "x=", x, ", y=", y, "z=", z
... 
>>> param1=(2,3)
>>> dict={"z":66}
>>> print_params(param1, dict)  #两个位置参数,没有关键字参数
x= (2, 3) , y= {'z': 66} z= 0
>>> print_params(*param1, **dict) #一个位置参数列表,一个关键字参数列表
x= 2 , y= 3 z= 66

获取全局变量字典 globals()
局部作用域内声明一个全局变量 global x。之后对x操作,全局变量会相应改变。
python函数是可以嵌套的,即可以在一个函数中定义另一个函数。

内建函数:
reduce会将序列中的前两个元素,与给定的函数联合使用,并且将他们的返回值和第三个元素继续联合使用,直到整个序列都处理完毕,并且得到一个最终结果。
map(func, seq[, seq,...]) 对序列中的每个元素应用函数
filter(func, seq) 返回其函数为真的元素的列表
reduce(func, seq[, initial]) 等同于func(func(func(func(func(seq[0], seq[1])), seq[2]), seq[3]), ...)
sum(seq) 返回seq中所有元素的和
apply(func[, args[, kwargs]]) 调用函数,可以提供参数

#reduce
>>> reduce(lambda x,y : x+y, range(11))
55

#map
>>> def addOne(x):
...     return x+1
... 
>>> map(addOne, range(5))
[1, 2, 3, 4, 5]

#filter
>>> def getNum(x):
...     if(x>2):
...             return 1
...     return 0
... 
>>> filter(getNum, range(5))
[3, 4]
原文地址:https://www.cnblogs.com/shoren/p/python-method.html