跟老齐学Python之传说中的函数编写条规,齐学python编写条规


关于函数的事情,总是说不完的,下面就罗列一些编写函数的注意事项。特别声明,这些事项不是我总结的,我是从一本名字为《Learning Python》的书里面抄过来的,顺便写成了汉语,当然,是按照自己的视角翻译的,里面也夹杂了一些自己的观点。看官也可以理解为源于《Learning Python》但又有点儿不同。
 •函数具有独立性。也就是常说的不要有太强的耦合性。要让函数能够独立于外部的东西。参数和return语句就是实现这种独立性的最好方法。
 •尽量不要使用全局变量,这也是让函数具有低耦合度的方法。全局变量虽然进行了函数内外通信,但是它强化了函数对外部的依赖,常常让函数的修改和程序调试比较麻烦。
 •如果参数的对象是可变类型的数据,在函数中,不要做对它的修改操作。当然,更多时候,参数传入的最好是不可变的。
 •函数实现的功能和目标要单一化。每个函数的开头,都要有简短的一句话来说明本函数的功能和目标。
 •函数不要太大,能小则小,根据前一条的原则,功能目标单一,则代码条数就小了。如果感觉有点大,看看能不能拆解开,分别为几个函数。
 •不要修改另外一个模块文件中的变量。这跟前面的道理是一样的,目的是降低耦合性。
 
小试一下递归

对于在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 "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in newsum
  File "<stdin>", line 5, in newsum
  File "<stdin>", line 5, in newsum
  File "<stdin>", 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

 所以,各位看官,在使用函数的时候,首先要把它放在对象的层面考量,它不是什么特殊的东西,尽管我们使用了不少篇幅讲述它,但它终归还是一个对象。


python的类中怎实现动态化函数?

给你这样一个例子吧,这个例子里面有动态增加类的函数。
声明一个类,类初始化的时候读取配置文件,根据配置列表加载特定目录下的模块下的函数,函数和模块同名,将此函数动态加载为类的成员函数。
代码如下所示:
class WinBAS(Bas):
def __init__(self):
self.__baslist = {}
self.__Init_Modules()
pass
def __Init_Modules(self):
import modplugs
for m in modplugs.__moduleset__:
mh = __import__('modules.' + m)# + '.' + m)
ma = getattr(mh, m)# + '.' + m)
ma = getattr(ma, m)
setattr(self.__class__, m, ma)
modplugs.py是模块配置文件如下:
__moduleset__ = [
'BAS_GetUserList',
]
然后建立目录modules下面建立一个空的__init__.py文件,把目录变为一个包,在modules目录下建立真正的BAS_GetUserList实现:BAS_GetUserList文件中有个BAS_GetUserList函数如下:
def BAS_GetUserList(self, strs):
return [0, strs]

这样WinBAS类就可以动态加入了BAS_GetUserList函数。
 

python中type()函数的问题

在2.6.7下运行上面的代码的显示结果:
Python 2.6.7 (r267:88850, Jul 10 2011, 09:55:27)
[GCC 4.6.1] on linux2
Type "copyright", "credits" or "license()" for more information.

****************************************************************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface. This connection is not visible on any external
interface and no data is sent to or received from the Internet.
****************************************************************

IDLE 2.6.7 ==== No Subprocess ====
>>> class Foo:pass
>>> foo=Foo()
>>> type(Foo)
<type 'classobj'>
>>> type(foo)
<type 'instance'>
>>>
>>> class Bar(object):pass
>>> bar=Bar()
>>> type(Bar)
<type 'type'>
>>> type(bar)
<class '__main__.Bar'>
>>>
在3.2.1下运行上面的代码的显示结果:
Python 3.2.1rc1 (default, May 18 2011, 18:34:29)
[GCC 4.6.1 20110507 (prerelease)] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====
>>> class Foo:pass
>>> foo=Foo()
>>> type(Foo)
<class 'type'>
>>> type(foo)
<class '__main__.Foo'>
>>>
>>> class Bar(object):pass

>>> bar=Bar()
>>> type(Bar)
<cl......余下全文>>
 

评论关闭