0

I have an object. Using __dict__, I can list down all the attributes of the object. Using __dir__, I can list down all the methods and attributes of the object. Is there a way to list down the attributes having setter properties declared in the class whose instance the object is.

vvvvv
  • 25,404
  • 19
  • 49
  • 81

2 Answers2

0

Use:

isinstance(getattr(type(object), 'attribute'), property)

Thanks to @a_guest and OP Rahul Nahata's own prior edit to the question.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
0

To check if an object is a property, use isinstance(value, property).

To list all the attributes and methods of a class, use: vars(MyClass).

Combine everything and get:

all_property_names = [name for name, value in vars(MyClass).items() if isinstance(value, property)]

With the following class,

class MyClass():
    def __init__(self):
        self._x = 1

    @property
    def x(self):
        return self._x

    def notproperty(self):
        return self.x

you would get:

# all_property_names
['x']
vvvvv
  • 25,404
  • 19
  • 49
  • 81