【问题标题】:How to get the name of attribute in python object? [duplicate]如何获取python对象中的属性名称? [复制]
【发布时间】:2011-09-08 07:28:01
【问题描述】:

例如我有下一个 python 类

class Myclass():
     a = int
     b = int

假设我不知道这个类的名称,所以我需要获取属性的名称? (“a”和“b”)

【问题讨论】:

    标签: python object attributes


    【解决方案1】:

    如果您想要所有(包括私有)属性,只需

    dir(Myclass)
    

    不过,以_ 开头的属性是私有的/内部的。例如,即使是简单的Myclass 也会有一个__module__ 和一个空的__doc__ 属性。要过滤掉这些,请使用

    filter(lambda aname: not aname.startswith('_'), dir(Myclass))
    

    【讨论】:

      猜你喜欢
      • 2018-06-08
      • 1970-01-01
      • 2012-04-18
      • 2022-08-23
      • 2017-05-21
      • 2012-02-16
      • 2015-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多