properties - Is there a succint way to get all the desciptors defined in a python class? -
is there simple method finding of descriptors in class type? if have code:
class descriptor(object): def __init__(self): self._name = '' def __get__(self, instance, owner): print "getting: %s" % self._name return self._name def __set__(self, instance, name): print "setting: %s" % name self._name = name.title() def __delete__(self, instance): print "deleting: %s" %self._name del self._name class contrivedexample(object): name = descriptor() date = descriptor()
how find out contrivedexample.name , contrivedexample.date exist outside class?
you can iterate through class' __dict__
property, stores of defined names within class, , check type of value each name.
descriptors = [m m,v in contrivedexample.__dict__.iteritems() if isinstance(v, descriptor)] # descriptors set ['name', 'date']
Comments
Post a Comment