检查python类属性

问题描述 投票:27回答:5

我需要一种检查类的方法,以便我可以安全地识别哪些属性是用户定义的类属性。问题是像dir(),inspect.getmembers()和friends这样的函数返回所有类属性,包括预定义的属性,如:__class____doc____dict____hash__。这当然是可以理解的,有人可能会说我可以只列出一个要忽略的命名成员列表,但不幸的是这些预定义的属性必然会随着Python的不同版本而改变,因此使我的项目在python项目中发生了变化 - 我不喜欢那样。

例:

>>> class A:
...   a=10
...   b=20
...   def __init__(self):
...     self.c=30
>>> dir(A)
['__doc__', '__init__', '__module__', 'a', 'b']
>>> get_user_attributes(A)
['a','b']

在上面的例子中,我想要一种安全的方法来只检索用户定义的类属性['a','b']而不是'c',因为它是一个实例属性。所以我的问题是......任何人都可以帮我解决上面的虚构功能get_user_attributes(cls)

附:我花了一些时间试图通过解析AST级别的类来解决问题,这很容易。但我找不到将已解析的对象转换为AST节点树的方法。我想一旦将一个类编译成字节码,就会丢弃所有的AST信息。

最好的问候雅各布

python class attributes introspection inspect
5个回答
28
投票

以下是艰难的方式。这是简单的方法。不知道为什么它不会早点发生在我身上。

import inspect

def get_user_attributes(cls):
    boring = dir(type('dummy', (object,), {}))
    return [item
            for item in inspect.getmembers(cls)
            if item[0] not in boring]

这是一个开始

def get_user_attributes(cls):
    boring = dir(type('dummy', (object,), {}))
    attrs = {}
    bases = reversed(inspect.getmro(cls))   
    for base in bases:
        if hasattr(base, '__dict__'):
            attrs.update(base.__dict__)
        elif hasattr(base, '__slots__'):
            if hasattr(base, base.__slots__[0]): 
                # We're dealing with a non-string sequence or one char string
                for item in base.__slots__:
                    attrs[item] = getattr(base, item)
            else: 
                # We're dealing with a single identifier as a string
                attrs[base.__slots__] = getattr(base, base.__slots__)
    for key in boring:
        del attrs['key']  # we can be sure it will be present so no need to guard this
    return attrs

这应该相当强大。本质上,它通过获取object的默认子类上的属性来忽略。然后它获取传递给它的类的mro并以相反的顺序遍历它,以便子类键可以覆盖超类键。它返回键值对的字典。如果你想要一个像inspect.getmembers那样的关键值元组列表,那么只需要在Python 3中返回attrs.items()list(attrs.items())

如果您实际上并不想遍历mro并且只想直接在子类上定义属性,那么它会更容易:

def get_user_attributes(cls):
    boring = dir(type('dummy', (object,), {}))
    if hasattr(cls, '__dict__'):
        attrs = cls.__dict__.copy()
    elif hasattr(cls, '__slots__'):
        if hasattr(base, base.__slots__[0]): 
            # We're dealing with a non-string sequence or one char string
            for item in base.__slots__:
                attrs[item] = getattr(base, item)
            else: 
                # We're dealing with a single identifier as a string
                attrs[base.__slots__] = getattr(base, base.__slots__)
    for key in boring:
        del attrs['key']  # we can be sure it will be present so no need to guard this
    return attrs

6
投票

'特殊属性'两端的双下划线是2.0之前的python的一部分。它们不太可能在不久的将来随时改变。

class Foo(object):
  a = 1
  b = 2

def get_attrs(klass):
  return [k for k in klass.__dict__.keys()
            if not k.startswith('__')
            and not k.endswith('__')]

print get_attrs(Foo)

['a','b']


3
投票

谢谢aaronasterling,你给了我需要的表达式:-)我的最终类属性检查器函数如下所示:

def get_user_attributes(cls,exclude_methods=True):
  base_attrs = dir(type('dummy', (object,), {}))
  this_cls_attrs = dir(cls)
  res = []
  for attr in this_cls_attrs:
    if base_attrs.count(attr) or (callable(getattr(cls,attr)) and exclude_methods):
      continue
    res += [attr]
  return res

要么仅返回类属性变量(排除methods = True),要么还检索方法。我的初始测试和上面的函数支持旧式和新式的python类。

/雅各布


2
投票

如果您使用新样式类,您可以简单地减去父类的属性吗?

class A(object):
    a = 10
    b = 20
    #...

def get_attrs(Foo):
    return [k for k in dir(Foo) if k not in dir(super(Foo))]

编辑:不完全。 __dict____module____weakref__在从对象继承时出现,但在对象本身中不存在。你可能会遇到这些特殊情况 - 我怀疑它们经常会发生变化。


1
投票

对不起,坏死的线程。令我感到惊讶的是,仍然没有简单的功能(或库)来处理2019年的常见用法。

我要感谢aaronasterling的想法。实际上,set容器提供了一种更直接的表达方式:

class dummy:    pass

def abridged_set_of_user_attributes(obj):
    return set(dir(obj))-set(dir(dummy))

def abridged_list_of_user_attributes(obj):
    return list(abridged_set_of_user_attributes(obj))

使用列表推导的原始解决方案实际上是两级循环,因为有两个in关键字复合,尽管只有一个for关键字使它看起来像工作少于它。

© www.soinside.com 2019 - 2024. All rights reserved.