2013-10-22 97 views

回答

4

有一個dir函數,該函數列出了對象的所有(很好,很多)屬性。但是,僅過濾功能是沒有問題的:

>>>import math 
>>>dir(math) 
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc'] 
>>> 
>>>[f for f in dir(math) if hasattr(getattr(math, f), '__call__')] # filter on functions 
['acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc'] 

您可能會發現Guide to Python introspection是一個有用的資源,以及這個問題:how to detect whether a python variable is a function?

+0

極其有用,在我的情況非常感謝。 – JAZs

3

這就是help()就派上用場了(如果你喜歡一個人可讀格式):

>>> import math 
>>> help(math) 
Help on built-in module math: 

NAME 
    math 

<snip> 

FUNCTIONS 
    acos(...) 
     acos(x) 

     Return the arc cosine (measured in radians) of x. 

    acosh(...) 
     acosh(x) 

     Return the hyperbolic arc cosine (measured in radians) of x. 

    asin(...) 
<snip> 
2

僅適用於內置功能:

from inspect import getmembers, isfunction 

functions_list = [o for o in getmembers(my_module, isfunction)] 
相關問題