Get name of current class?

后端 未结 7 2163
有刺的猬
有刺的猬 2020-12-04 23:21

How do I get the name of the class I am currently in?

Example:

def get_input(class_name):
    [do things]
    return class_name_result

class foo():
         


        
相关标签:
7条回答
  • 2020-12-04 23:43
    import sys
    
    def class_meta(frame):
        class_context = '__module__' in frame.f_locals
        assert class_context, 'Frame is not a class context'
    
        module_name = frame.f_locals['__module__']
        class_name = frame.f_code.co_name
        return module_name, class_name
    
    def print_class_path():
        print('%s.%s' % class_meta(sys._getframe(1)))
    
    class MyClass(object):
        print_class_path()
    
    0 讨论(0)
提交回复
热议问题