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.
Asked
Active
Viewed 78 times
0

vvvvv
- 25,404
- 19
- 49
- 81

Rahul Nahata
- 3
- 4
-
what do you mean by "the setter functions" ? – bruno desthuilliers Jul 04 '18 at 07:56
-
You can do `filter(lambda x: isinstance(x, property), map(lambda y: getattr(type(obj), y), dir(type(obj))))`. – a_guest Jul 04 '18 at 08:00
-
I meant the setter property definition. The function with @property decorator – Rahul Nahata Jul 04 '18 at 08:37
2 Answers
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