Home > Net >  Get subclass name python [duplicate]
Get subclass name python [duplicate]

Time:09-17

I have 2 built-in classes in library.

class Main:
# some code
def __repr__(self):
    #here i should return class where it was called

class MainGroup:
# some code

class Form(MainGroup):
   name = Main()

Now, I need to get 'Form' - name of a class. I should get it when I call Form.name . I thought of creating __ repr __ function, but the problem is, I need to get class name, where Main() is called.

Output should be:

print(Form.name)
Output: 'Form'

But I am not sure how to get 'Form' output from a class called in my Form class.

CodePudding user response:

Classes and functions have a __name__ attribute just for storing the definition name as a string: Form.__name__ is "Form".

You can't really make a class attribute reference its own class in the definition, but you can do this:

class Form:
    pass

Form.name = Form.__name__
  • Related