inspect.isfunction对所有模块都不起作用?

时间:2014-03-15 19:50:36

标签: python python-module inspect

我正在尝试使用Is it possible to list all functions in a module?的答案列出一系列模块中的函数。但在我的翻译中,我得到如下:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> import math
>>> math.pow(5,4)
625.0
>>> inspect.getmembers(math, inspect.isfunction)
[]
>>> inspect.getmembers(inspect, inspect.isfunction)
[('_check_class', <function _check_class at 0x00C9F9C0>), ('_check_instance', <f
unction _check_instance at 0x00C9F978>), ('_get_user_defined_method', <function
_get_user_defined_method at 0x00C9FB70>), ('_getfullargs', <function _getfullarg
s at 0x00C6A4F8>), ('_is_type', <function _is_type at 0x00C9FA08>), ('_missing_a
rguments', <function _missing_arguments at 0x00C9F198>), ('_shadowed_dict', <fun
ction _shadowed_dict at 0x00C9FA50>)... foo, bar, etc]

>>> math
<module 'math' (built-in)>
>>> inspect
<module 'inspect' from 'E:\\Python33\\lib\\inspect.py'>

>>> import win32api
>>> inspect.getmembers(win32api, inspect.isfunction)
[]
>>> win32api
<module 'win32api' from 'E:\\Python33\\lib\\site-packages\\win32\\win32api.pyd'>

按照惯例,我假设有一个令人眩目的明显原因,为什么这不能列出我尝试的一半模块中的所有功能。我能猜到的最好的是isfunction()只适用于.py模块吗?如果存在与我的愚蠢无关的先天问题,有没有办法纠正这个问题?

这显然是isfunction()的问题,因为getmembers似乎工作正常:

>>> import math
>>> import inspect
>>> inspect.getmembers(math)
[('__doc__', 'This module is always available.  It provides access to the\nmathe
matical functions defined by the C standard.'), ('__loader__', <class '_frozen_i
mportlib.BuiltinImporter'>), ('__name__', 'math'), ('__package__', ''), ('acos',
 <built-in function acos>), ('acosh', <built-in function acosh>), ('asin', <buil
t-in function asin>)... foo, bar, etc]

我知道有一个可以使用的dir(),但它并不是那么整洁。

2 个答案:

答案 0 :(得分:4)

C定义的函数不是用户定义的function类型的实例;它们是用C语言定义的,你可以使用inspect.isbuiltin()

列出模块中所有功能的万无一失的方法是使用inspect.isroutine()代替:

  

如果对象是用户定义的函数或内置函数或方法,则返回true。

math模块上的演示:

>>> import inspect
>>> import math
>>> inspect.getmembers(math, inspect.isroutine)
[('acos', <built-in function acos>), ('acosh', <built-in function acosh>), ('asin', <built-in function asin>), ('asinh', <built-in function asinh>), ('atan', <built-in function atan>), ('atan2', <built-in function atan2>), ('atanh', <built-in function atanh>), ('ceil', <built-in function ceil>), ('copysign', <built-in function copysign>), ('cos', <built-in function cos>), ('cosh', <built-in function cosh>), ('degrees', <built-in function degrees>), ('erf', <built-in function erf>), ('erfc', <built-in function erfc>), ('exp', <built-in function exp>), ('expm1', <built-in function expm1>), ('fabs', <built-in function fabs>), ('factorial', <built-in function factorial>), ('floor', <built-in function floor>), ('fmod', <built-in function fmod>), ('frexp', <built-in function frexp>), ('fsum', <built-in function fsum>), ('gamma', <built-in function gamma>), ('hypot', <built-in function hypot>), ('isinf', <built-in function isinf>), ('isnan', <built-in function isnan>), ('ldexp', <built-in function ldexp>), ('lgamma', <built-in function lgamma>), ('log', <built-in function log>), ('log10', <built-in function log10>), ('log1p', <built-in function log1p>), ('modf', <built-in function modf>), ('pow', <built-in function pow>), ('radians', <built-in function radians>), ('sin', <built-in function sin>), ('sinh', <built-in function sinh>), ('sqrt', <built-in function sqrt>), ('tan', <built-in function tan>), ('tanh', <built-in function tanh>), ('trunc', <built-in function trunc>)]

注意每个函数对象的字符串表示的built-in部分。

如果inspect.isbuiltin()inspect.isfunction()inspect.ismethod()inspect.ismethoddescriptor()中的一个返回True,则

isroutine()会返回true。

答案 1 :(得分:1)

您是否阅读了the documentation for isfunction(强调添加):

  

如果对象是 Python 函数

,则返回true

所以不,它对内置函数不起作用。

您可以使用inspect.isroutine代替您的考试。这仍然会遗漏定义__call__的对象,这些对象可能是您想要的,也可能不是。您也可以使用callable进行测试,这也可能是您想要的,也可能不是。知道你想要考虑什么样的对象&#34;函数&#34;并不总是一项简单的任务,因为可能有一些对象意味着像函数一样但实际上并不是函数。