这段话的意思是,在Python中,函数和其他对象一样。
例如:
In [5]: def f(): pass
现在,
f
是一个类型为
function
的对象:
In [6]: type(f)
Out[6]: function
如果你仔细地查看它,它包括很多字段:
In [7]: dir(f)
Out[7]:
['__call__',
...
'func_closure',
'func_code',
'func_defaults',
'func_dict',
'func_doc',
'func_globals',
'func_name']
拿一个例子来说,f.func_name
是函数的名称:
In [8]: f.func_name
Out[8]: 'f'
而 f.func_code
包含了代码:
In [9]: f.func_code
Out[9]: <code object f at 0x11b5ad0, file "<ipython-input-5-87d1450e1c01>", line 1>
如果你真的很好奇,你可以进一步深入了解:
In [10]: dir(f.func_code)
Out[10]:
['__class__',
...
'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']
等等。
(上面的输出是使用Python 2.7.3生成的。)