所有这一切,说的是,在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生产。)
来源
2014-03-28 06:46:05
NPE
glglgl - 按要求 – overexchange
我假设你想从其他一些问题继续单独查询升高。堆栈溢出的每个问题应该是自包含的。您无需访问任何链接即可使这个问题清晰易懂。按照现状,很难说出你的实际问题是什么。 –
我们在python中处理的所有东西都是'object'。 – fledgling