检查python类属性
问题描述
我需要一种检查类的方法,以便我可以安全地识别哪些属性是用户定义的类属性.问题是 dir()、inspect.getmembers() 和朋友之类的函数会返回所有类属性,包括预定义的属性,例如:__class__
、__doc__
、__dict__
,__hash__
.这当然是可以理解的,有人可能会争辩说我可以只列出一个要忽略的命名成员,但不幸的是,这些预定义的属性必然会随着 Python 的不同版本而改变,因此我的项目很容易在 python 项目中发生变化- 我不喜欢那样.
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__
. 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.
示例:
>>> 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)
?
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. So my question is... Can anyone help me with the above fictive function get_user_attributes(cls)
?
我花了一些时间尝试通过解析 AST 级别的类来解决这个问题,这很容易.但是我找不到将已解析的对象转换为 AST 节点树的方法.我想一旦一个类被编译成字节码,所有的 AST 信息都会被丢弃.
I have spent some time trying to solve the problem by parsing the class in AST level which would be very easy. But I can't find a way to convert already parsed objects to an AST node tree. I guess all AST info is discarded once a class has been compiled into bytecode.
解决方案
下面是困难的方法.这是简单的方法.不知道为什么我没有早点想到.
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
这应该是相当健壮的.本质上,它的工作原理是让 object
的默认子类上的属性被忽略.然后它获取传递给它的类的 mro 并以相反的顺序遍历它,以便子类键可以覆盖超类键.它返回一个键值对字典.如果您想要 inspect.getmembers
中的键值元组列表,则只需返回 attrs.items()
或 list(attrs.items())
在 Python 3 中.
This should be fairly robust. Essentially, it works by getting the attributes that are on a default subclass of object
to ignore. 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. 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.
如果您实际上不想遍历 mro,而只想直接在子类上定义属性,那么会更容易:
If you don't actually want to traverse the mro and just want attributes defined directly on the subclass then it's easier:
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
相关文章