简体   繁体   English

检查python类属性

[英]Inspect python class attributes

I need a way to inspect a class so I can safely identify which attributes are user-defined class attributes.我需要一种检查类的方法,以便我可以安全地识别哪些属性是用户定义的类属性。 The problem is that functions like dir(), inspect.getmembers() and friends return all class attributes including the pre-defined ones like: __class__ , __doc__ , __dict__ , __hash__ .问题是像 dir()、inspect.getmembers() 和朋友这样的函数返回所有类属性,包括预定义的属性,如: __class____doc____dict____hash__ This is of course understandable, and one could argue that I could just make a list of named members to ignore, but unfortunately these pre-defined attributes are bound to change with different versions of Python therefore making my project volnerable to changed in the python project - and I don't like that.这当然是可以理解的,有人可能会争辩说我可以只列出要忽略的命名成员列表,但不幸的是,这些预定义的属性必然会随着 Python 的不同版本而改变,因此使我的项目容易在 Python 项目中更改- 我不喜欢那样。

example:例子:

>>> 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']

In the example above I want a safe way to retrieve only the user-defined class attributes ['a','b'] not 'c' as it is an instance attribute.在上面的示例中,我想要一种安全的方法来仅检索用户定义的类属性 ['a','b'] 而不是 'c',因为它是一个实例属性。 So my question is... Can anyone help me with the above fictive function get_user_attributes(cls) ?所以我的问题是......任何人都可以帮助我使用上述虚构函数get_user_attributes(cls)吗?

PS I have spent some time trying to solve the problem by parsing the class in AST level which would be very easy. PS 我花了一些时间试图通过解析 AST 级别的类来解决问题,这很容易。 But I can't find a way to convert already parsed objects to an AST node tree.但是我找不到将已经解析的对象转换为 AST 节点树的方法。 I guess all AST info is discarded once a class has been compiled into bytecode.我猜一旦一个类被编译成字节码,所有的 AST 信息都会被丢弃。

Best regards Jakob最好的问候雅各布

Below is the hard way.下面是艰难的方法。 Here's the easy way.这是简单的方法。 Don't know why it didn't occur to me sooner.不知道为什么我没有早点想到。

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]

Here's a start这是一个开始

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

This should be fairly robust.这应该是相当健壮的。 Essentially, it works by getting the attributes that are on a default subclass of object to ignore.本质上,它的工作原理是获取要忽略的object的默认子类上的属性。 It then gets the mro of the class that's passed to it and traverses it in reverse order so that subclass keys can overwrite superclass keys.然后它获取传递给它的类的 mro 并以相反的顺序遍历它,以便子类键可以覆盖超类键。 It returns a dictionary of key-value pairs.它返回一个键值对字典。 If you want a list of key, value tuples like in inspect.getmembers then just return either attrs.items() or list(attrs.items()) in Python 3.如果你想要一个像inspect.getmembers那样的键值元组列表,那么只需在Python 3中返回attrs.items()list(attrs.items())

If you don't actually want to traverse the mro and just want attributes defined directly on the subclass then it's easier:如果您实际上不想遍历 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

Double underscores on both ends of 'special attributes' have been a part of python before 2.0. “特殊属性”两端的双下划线在 2.0 之前是 python 的一部分。 It would be very unlikely that they would change that any time in the near future.他们不太可能在不久的将来改变这种情况。

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'] ['a', 'b']

Thanks aaronasterling, you gave me the expression i needed :-) My final class attribute inspector function looks like this:谢谢 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

Either return class attribute variabels only (exclude_methods=True) or also retrieve the methods.仅返回类属性变量(exclude_methods=True)或检索方法。 My initial tests og the above function supports both old and new-style python classes.我的初始测试 og 上面的函数支持旧式和新式 python 类。

/ Jakob / 雅各布

If you use new style classes, could you simply subtract the attributes of the parent class?如果使用新的样式类,是否可以简单地减去父类的属性?

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))]

Edit: Not quite.编辑:不完全是。 __dict__ , __module__ and __weakref__ appear when inheriting from object, but aren't there in object itself. __dict____module____weakref__在从 object 继承时出现,但在 object 本身中不存在。 You could special case these--I doubt they'd change very often.你可以对这些进行特殊处理——我怀疑它们会经常改变。

Sorry for necro-bumping the thread.对不起,我死伤了线程。 I'm surprised that there's still no simple function (or a library) to handle such common usage as of 2019.令我惊讶的是,截至 2019 年,仍然没有简单的函数(或库)来处理这种常见用法。

I'd like to thank aaronasterling for the idea.我要感谢 aaronasterling 的想法。 Actually, set container provides a more straightforward way to express it:实际上, set container 提供了一种更直接的表达方式:

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))

The original solution using list comprehension is actually two level of loops because there are two in keyword compounded, despite having only one for keyword made it look like less work than it is.使用列表理解原来的解决方案实际上是循环的二级因为有两个in关键字加剧,尽管只有一个for关键字使它看起来像较少的工作比它。

This worked for me to include user defined attributes with __ that might be be found in cls.__dict__这对我__ ,可以在cls.__dict__包含带有__用户定义属性

import inspect

class A:
    __a = True
    
    def __init__(self, _a, b, c):
        self._a = _a
        self.b = b
        self.c = c 

    def test(self):
        return False

cls = A(1, 2, 3)

members = inspect.getmembers(cls, predicate=lambda x: not inspect.ismethod(x))
attrs = set(dict(members).keys()).intersection(set(cls.__dict__.keys()))
__attrs = {m[0] for m in members if m[0].startswith(f'_{cls.__class__.__name__}')}
attrs.update(__attrs)

This will correctly yield: {'_A__a', '_a', 'b', 'c'}这将正确产生: {'_A__a', '_a', 'b', 'c'}

You can update to clean the cls.__class__.__name__ if you wish如果您愿意,您可以更新以清理cls.__class__.__name__

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM