简体   繁体   English

如何获取属于Python 3中特定类的所有属性(继承的属性除外)的值

[英]How to get values of all properties (except inherited ones) that belong to specific class in Python 3

How (in Python 3) to get values of all properties that belong to specific class. 如何(在Python 3中)获取属于特定类的所有属性的值。 I need ONLY those values (properties) that defined in specific class without inherited ones. 我只需要在特定类中定义的那些值(属性),而无需继承这些值(属性)。

Here is some example: 这是一些例子:

class A(object):
    def __init__(self, color):
        self._color = color

    @property
    def color(self):
        return self._color

class B(A):
    def __init__(self, color, height, width):
        super().__init__(color)
        self._height = height
        self._width = width

    @property
    def height(self):
        return self._height

    @property
    def width(self):
        return self._width

and here is a code for fetching all values (including inherited): 这是用于获取所有值(包括继承的值)的代码:

b_inst = B('red', 10, 20)
val = [{p: b_inst.__getattribute__(p)} for p in dir(B)
       if isinstance(getattr(B, p), property)]

print(val)

>> [{'color': 'red'}, {'height': 10}, {'width': 20}]

Now, I just want to retrieve values of properties defined ONLY in class B , ie height and width . 现在,我只想检索仅在class B定义的属性的值,即heightwidth

Note that in Python "property" has a very specific meaning (the builtin property type). 请注意,在Python中,“属性”具有非常特定的含义(内置property类型)。 If you're only concerned about this then you just have to lookup your child class's __dict__ : 如果您仅对此担心,则只需查找子类的__dict__

val = [p.__get__(c) for k, p in type(c).__dict__.items() if isinstance(p, property)]

If you want something that works on any arbitrary attribute then what you ask for is just not possible, since Python objects (with a few exceptions) are dict-based (vs struct-based like in C++ or Java) and dynamic (any piece of code can add / remove arbitrary attributes on a per-instance basis) so there's no fix schema nor class-level definition of what attributes a given object may or not possess. 如果您想要对任何任意属性都适用的东西,那么您所要求的就是不可能的,因为Python对象(有一些例外)是基于dict的(与C ++或Java中的基于struct的相比)和动态的(基于代码可以在每个实例的基础上添加/删除任意属性),因此对于给定对象可能具有或不具有什么属性,没有固定的架构或类级别的定义。

If property == to attribute , from the doc : 如果属性==为attribute ,则来自doc

Attribute assignments and deletions update the instance's dictionary, never a class's dictionary. 属性分配和删除将更新实例的字典,而不会更新类的字典。

So even you do self._color = color on a base class, you are assigning that attribute/property to the instance of the B class. 因此,即使您在基类上执行self._color = color ,您self._color = color将该属性/属性分配给B类的实例。

So, at least that you save info tied to the attribute/property for consult latter, I don't think that given how Python works, you will be able to determine to what class the method in which a property was assigned belong to , that is how your question means in Python. 因此,至少您保存了与属性/属性相关的信息以供以后参考,我认为,鉴于Python的工作原理,您将无法确定分配属性的方法属于哪个类 ,即是您的问题在Python中的含义。

I hope that this helps you to clarify the OOP model of Python. 我希望这可以帮助您阐明Python的OOP模型。

If you are referring to Python's properties , then the @bruno answer is perfect. 如果您指的是Python的属性 ,那么@bruno答案是完美的。 I will still leave this answer here because is kind of related to your topic and could be useful for readers. 我仍然在这里留下这个答案,因为这与您的主题有关,可能对读者有用。

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

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