【问题标题】:how to find user defined functions in python script如何在python脚本中查找用户定义的函数
【发布时间】:2015-07-24 06:11:42
【问题描述】:

python 程序中有内置函数和用户定义的函数。 我希望列出该程序中所有用户定义的函数。 任何人都可以建议我如何做到这一点?

示例:

class sample():

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

我需要这样的输出:

func1

func2

【问题讨论】:

  • 哪个程序?有示例代码吗?
  • 任何具有用户定义函数数量的python代码。
  • 12 可能会有所帮助。

标签: function python-2.7 user-defined-functions built-in


【解决方案1】:

Python 3.x 中的一个廉价技巧是这样的:

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

返回以双下划线开头的非魔法函数。

['func1', 'func2']

【讨论】:

    【解决方案2】:

    这并不完全正确。 dir() 函数“试图产生最相关的,而不是完整的信息”。 来源:

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

    Is it possible to list all functions in a module?

    【讨论】:

      猜你喜欢
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      • 2022-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多