Python编程实用指南:函数、数据处理与系统管理
1. 回调函数与函数对象
回调函数和传递函数的概念可能对一些人来说比较陌生,但深入理解它们是很有价值的。在Python中,函数是“一等公民”,这意味着可以像操作对象一样传递和处理函数。
1.1 函数作为一等对象示例
以下代码展示了函数作为一等对象的特性:
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']
通过上述代码可
超级会员免费看
订阅专栏 解锁全文
23万+

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



