在Python中获取类名有几种常见的方法:
1. 使用 `type()` 函数:
class MyClass:passobj = MyClass()print(type(obj).__name__) 输出:MyClass
2. 使用 `__class__` 属性:
class MyClass:def get_class_name(self):return self.__class__.__name__obj = MyClass()print(obj.get_class_name()) 输出:MyClass
3. 使用 `isinstance()` 函数:

class MyClass:passobj = MyClass()print(isinstance(obj, MyClass)) 输出:True
4. 使用 `inspect` 模块:
import inspectclass MyClass:def hello(self):frame = inspect.currentframe()method_name = frame.f_code.co_nameclass_name = self.__class__.__name__print(f"The name of the method is {method_name} and the name of the class is {class_name}")if __name__ == "__main__":h = MyClass()h.hello()
以上方法可以帮助你在Python中获取类名。
