关于函数的事情,总是说不完的,下面就罗列一些编写函数的注意事项。特别声明,这些事项不是我总结的,我是从一本名字为《Learning Python》的书里面抄过来的,顺便写成了汉语,当然,是按照自己的视角翻译的,里面也夹杂了一些自己的观点。看官也可以理解为源于《Learning Python》但又有点儿不同。
对于在python中使用递归,我一项持谨慎态度,能不用就不用,为什么呢?一方面深恐自己学艺不精,另外,递归不仅消耗资源,而且很多时候速度也不如for循环快。
不过,做为程序员,递归还是需要了解的。这里就列举一个简单的例子。
>>> def newsum(lst):
... if not lst:
... return 0
... else:
... return lst[0] + newsum(lst[1:])
...
>>> newsum([1,2,3])
6
这是一个对list进行求和的函数(看官可能想到了,不是在python中有一个sum内置函数来求和么?为什么要自己写呢?是的,在实际的编程中,没有必要自己写,用sum就可以了。这里用这个例子,纯粹是为了说明递归,没有编程实践的意义),当然,我没有判断传给函数的参数是否为完全由数字组成的list,所以,如果输入的list中字母,就会编程这样了:
>>> newsum([1,2,3,\'q\'])
Traceback (most recent call last):
File \"\", line 1, in
File \"\", line 5, in newsum
File \"\", line 5, in newsum
File \"\", line 5, in newsum
File \"\", line 5, in newsum
TypeError: cannot concatenate \'str\' and \'int\' objects
这就是本函数的缺憾了。但是,为了说明递归,我们就顾不了这么多了。暂且忽略这个缺憾。看官注意上面的函数中,有一句:return lst(0)+newsum(lst[1:]),在这句话中,又调用了一边函数本身。对了,这就递归,在函数中调用本函数自己。当然,区别在于传入的参数有变化了。为了清除函数的调用流程,我们可以将每次传入的参数打印出来:
>>> def newsum(lst):
... print lst
... if not lst:
... return 0
... else:
... return lst[0] + newsum(lst[1:])
...
>>> newsum([1,2,3])
[1, 2, 3]
[2, 3]
[3]
[]
6
这就是递归了。
其实,看官或许已经想到了,即使不用sum,也可以用for来事项上述操作。
>>> lst = [1,2,3]
>>> sum_result = 0
>>> for x in lst: sum_result += x
...
>>> sum_result
6
还记得,在第一部分学习的时候,不断强调的:变量无类型,数据有类型,那时候遇到的数据包括字符串、数值、列表、元组、字典、文件,这些东西,都被视为对象。函数跟它们类似,也是对象。因此就可以像以前的对象一样进行赋值、传递给其它函数、嵌入到数据结构、从一个函数返回给另一个函数等等面向对象的操作。当然,函数这个对象也有特殊性,就是它可以由一个函数表达式后面的括号中的列表参数调用。
>>> def newsum(lst): #依然以这个递归的函数为例
... print lst
... if not lst:
... return 0
... else:
... return lst[0] + newsum(lst[1:])
...
>>> lst = [1,2,3]
>>> newsum(lst) #这是前面已经常用的方法
[1, 2, 3]
[2, 3]
[3]
[]
6
>>> recusion_fun = newsum #通过赋值语句,让变量recusion_fun也引用了函数newsum(lst)对象
>>> recusion_fun(lst) #从而变量能够实现等同函数调用的操作
[1, 2, 3]
[2, 3]
[3]
[]
6
再看一个例子,在这个例子中,一定要谨记函数是对象。看官曾记否?在list中,可以容纳任何对象,那么,是否能够容纳一个函数中呢?
>>> fun_list = [(newsum,[1,2,3]),(newsum,[1,2,3,4,5])]
>>> for fun,arg in fun_list:
... fun(arg)
...
[1, 2, 3]
[2, 3]
[3]
[]
6
[1, 2, 3, 4, 5]
[2, 3, 4, 5]
[3, 4, 5]
[4, 5]
[5]
[]
15
函数,真的就是对象啊。
既然是对象,就可以用dir(object)方式查看有关信息喽:
>>> dir(newsum)
[\'__call__\', \'__class__\', \'__closure__\', \'__code__\', \'__defaults__\', \'__delattr__\', \'__dict__\', \'__doc__\', \'__format__\', \'__get__\', \'__getattribute__\', \'__globals__\', \'__hash__\', \'__init__\', \'__module__\', \'__name__\', \'__new__\', \'__reduce__\', \'__reduce_ex__\', \'__repr__\', \'__setattr__\', \'__sizeof__\', \'__str__\', \'__subclasshook__\', \'func_closure\', \'func_code\', \'func_defaults\', \'func_dict\', \'func_doc\', \'func_globals\', \'func_name\']
>>> dir(newsum.__code__)
[\'__class__\', \'__cmp__\', \'__delattr__\', \'__doc__\', \'__eq__\', \'__format__\', \'__ge__\', \'__getattribute__\', \'__gt__\', \'__hash__\', \'__init__\', \'__le__\', \'__lt__\', \'__ne__\', \'__new__\', \'__reduce__\', \'__reduce_ex__\', \'__repr__\', \'__setattr__\', \'__sizeof__\', \'__str__\', \'__subclasshook__\', \'co_argcount\', \'co_cellvars\', \'co_code\', \'co_consts\', \'co_filename\', \'co_firstlineno\', \'co_flags\', \'co_freevars\', \'co_lnotab\', \'co_name\', \'co_names\', \'co_nlocals\', \'co_stacksize\', \'co_varnames\']
>>> newsum.__code__.__doc__
\'code(argcount, nlocals, stacksize, flags, codestring, constants, names,\\n varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\\n\\nCreate a code object. Not for the faint of heart.\'
>>> newsum.__code__.co_varnames
(\'lst\',)
>>> newsum.__code__.co_argcount
1
所以,各位看官,在使用函数的时候,首先要把它放在对象的层面考量,它不是什么特殊的东西,尽管我们使用了不少篇幅讲述它,但它终归还是一个对象。