Как перебрать атрибуты объекта, определенного getters или @property? - PullRequest
0 голосов
/ 26 марта 2019

Я знаю, как перебирать атрибуты объекта.Но это исключает атрибуты, реализованные через методы получения / установки или @property декораторы.

Как я могу перебрать их?

class MyClass:

    def __init__(self):
        self.foo = "bar"

    @property
    def myprop(self):
        return "hello"


my_instance = MyClass()

for i in my_instance.__dict__:
    print("object has attribute %s" % i)

Этот маленький скрипт печатает:

object has attribute foo

Хотя мне нужен скрипт, который печатает:

object has attribute foo
object has attribute myprop

Ответы [ 2 ]

0 голосов
/ 26 марта 2019

Вы можете искать property экземпляры в классе объекта:

def get_properties(obj):
    cls = type(obj)
    props = {}
    for k in dir(cls):
        attr = getattr(cls, k)
        # Check that it is a property with a getter
        if isinstance(attr, property) and attr.fget:
            val = attr.fget(obj)
            props[k] = attr.fget(obj)
    return props

class A(object):
    def __init__(self, p):
        self._p = p
    @property
    def p(self):
        return self._p
    p2 = property(fget=lambda self: 2 * self._p)

a = A(1)
a_props = get_properties(a)
print(a_props)
# {'p': 1, 'p2': 2}
0 голосов
/ 26 марта 2019

Вы можете использовать dir() для получения всех атрибутов, хотя это будет включать методы, унаследованные от базовых классов, включая все более сложные методы объекта:

class MyClass:

    def __init__(self):
        self.foo = "bar"

    @property
    def myprop(self):
        return "hello"


my_instance = MyClass()

for i in dir(my_instance):
    print("object has attribute %s" % i)

print:

object has attribute __class__
object has attribute __delattr__
object has attribute __dict__
object has attribute __dir__
object has attribute __doc__
object has attribute __eq__
object has attribute __format__
object has attribute __ge__
object has attribute __getattribute__
object has attribute __gt__
object has attribute __hash__
object has attribute __init__
object has attribute __init_subclass__
object has attribute __le__
object has attribute __lt__
object has attribute __module__
object has attribute __ne__
object has attribute __new__
object has attribute __reduce__
object has attribute __reduce_ex__
object has attribute __repr__
object has attribute __setattr__
object has attribute __sizeof__
object has attribute __str__
object has attribute __subclasshook__
object has attribute __weakref__
object has attribute foo
object has attribute myprop

Вы можете исключить некоторые с помощью строковых операций:

for i in dir(my_instance):
    if i.startswith("__"):
        continue
    print("object has attribute %s" % i)

печать:

object has attribute foo
object has attribute myprop
...