There was no Japanese article that describes how to get the class name from the class object itself, so I will write it as a memorandum.
Even if I googled, most of them were obtained using this method.
class A:
pass
a = A()
print(a.__class__.__name__)
>>> A
You can get it with __qualname__ implemented from Python 3.3.
Postscript: It seems that you can also get it with B.__name__. Thank you, @shiracamus.
Also, __qualname__ can be simply written as __qualname__ instead of B.__qualname__ in a class object.
class B:
pass
print(B.__qualname__)
>>> B
#Postscript
class C:
qualname = __qualname__
print(C.__name__)
print(C.qualname)
>>> C
>>> C
If it is ~~ __qualname__, you can get it in the class object or in the function that takes the class object as an argument. ~~
Addendum: As I added earlier, it seems that you can also get it by using __name__.
[Get the name of the current class? ](Https://www.it-swarm.dev/ja/python/ Get the name of the current class? /940596062/)
Recommended Posts