Python: search global classes in a single module file -
i have python 2.x module.py
file looks this:
class a(object): keyword = 'class a' class b(a): keyword = 'class b' class c(object): pass def list_class_keywords(): name in globals(): print name, hasattr(name, 'keyword') if __name__ == '__main__': list_class_keywords()
in list_class_keywords()
, i'm looping through of objects of file module , testing if object has attribute keyword
. isn't working, since name
string. how should rewrite list_classes
i'm looking for?
update: ignacio hint. here updated code:
def list_class_keywords(): global_dict = globals() name in global_dict: obj = global_dict[name] print name, hasattr(obj, 'keyword')
class a(object): keyword = 'class a' class b(a): keyword = 'class b' class c(object): pass def list_class_keywords(): name, obj in globals().items(): print name, hasattr(obj, 'keyword') if __name__ == '__main__': list_class_keywords()
Comments
Post a Comment