如何在python脚本中查找用户定义的函数

问题描述 投票:1回答:2

python程序内置了函数和用户定义的函数。我希望列出该程序中所有用户定义的函数。有谁能建议我怎么做?

例:

class sample():

     def __init__(self):
         .....some code....
     def func1():
         ....some operation...
     def func2():
         ...some operation..

我需要像这样的输出:

func1

func2
function python-2.7 user-defined-functions built-in
2个回答
0
投票

这不是严格意义上的。 dir()函数“试图产生最相关的信息,而不是完整的信息”。资源:

How do I get list of methods in a Python class?

Is it possible to list all functions in a module?


0
投票

Python 3.x中的一个便宜技巧是:

sample = Sample()
[i for i in dir(sample) if not i.startswith("__")]

返回

['func1', 'func2']
© www.soinside.com 2019 - 2024. All rights reserved.