【问题标题】:Can i get all methods of a python object?我可以获得python对象的所有方法吗?
【发布时间】:2015-10-04 02:03:37
【问题描述】:

我想获取一个对象的所有方法。我知道函数dir,但它返回所有(方法、属性、元数据)。

我试过了:

[x for x in dir(obj) if "_" not in x]

但它不能正常工作。

我该怎么做?

【问题讨论】:

标签: python


【解决方案1】:

您需要查看inspect。例如

inspect.getmembers(object, inspect.ismethod)

它只返回方法。

【讨论】:

    【解决方案2】:

    您可以过滤dir结果

    [method for method in dir(obj) if callable(getattr(obj, method))]
    

    【讨论】:

      猜你喜欢
      • 2011-07-13
      • 1970-01-01
      • 2019-02-16
      • 2011-04-10
      • 2020-03-08
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多