gpt4 book ai didi

python - inspect.isfunction 不适用于所有模块?

转载 作者:行者123 更新时间:2023-11-28 21:55:47 26 4
gpt4 key购买 nike

我正在尝试使用 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() 可以使用,但它并不是那么整洁。

最佳答案

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

列出模块中所有函数的简单方法是使用 inspect.isroutine()相反:

Return true if the object is a user-defined or built-in function or method.

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>)]

注意每个函数对象的字符串表示的内置部分。

isroutine() 如果 inspect.isbuiltin() 之一返回真, inspect.isfunction() , inspect.ismethod()inspect.ismethoddescriptor()返回真。

关于python - inspect.isfunction 不适用于所有模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22428910/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com