Reputation: 1269
I have a class that looks like:
class Test(object):
@property
def prop1(self):
return 5
@property
def prop2(self):
return 10
How do I get back the properties I implemented? For example, [prop1, prop2]
.
I have tried vars()
and dir()
but these seem to also return hidden/special methods.
Is the only way to do this to parse through the results that don't have underscores for say dir()
?
Upvotes: 0
Views: 125
Reputation: 159
You can use following:
def isprop(v):
return isinstance(v, property)
propnames = [name for (name, value) in inspect.getmembers(Test, isprop)]
Upvotes: 0
Reputation: 6631
Try this
>>>[ k for k,v in Test.__dict__.items() if isinstance(v, property) ]
['prop1', 'prop2']
Since property is a type we can use isinstance
to find it in the class' internal dictionary.
Upvotes: 3