Python编程:从回调函数到系统管理的全面指南
1. 回调函数与一等公民函数
1.1 回调函数概念
回调函数和传递函数的概念可能对你来说比较陌生,但值得深入研究。在Python中,函数是“一等公民”,这意味着你可以像操作对象一样传递和处理函数,因为它们本质上就是对象。
1.2 一等公民函数示例
以下代码展示了函数作为一等公民的特性:
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_n
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



