Python编程中的回调函数与多元应用技巧
回调函数概念
回调函数和传递函数的概念可能对许多人来说比较陌生,但深入了解它是很值得的。在Python里,函数属于“一等公民”,这意味着可以像操作对象一样传递和处理函数,因为函数本身就是对象。
下面是一个展示函数作为一等公民的示例:
In [1]: def foo(): ...: print foo ...: ...: In [2]: foo Out[2]: <function foo at 0x1233270> In [3]: type(foo) Out[3]: <type 'function'> In [4]: dir(foo) Out[4]: ['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__', '__getattribute__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']在这个例子中,仅仅提及函数名foo