【问题标题】:Is there Python Class instance have an attribute to get the class name? [duplicate]是否有 Python Class 实例具有获取类名的属性? [复制]
【发布时间】:2020-02-09 15:58:53
【问题描述】:

我有以下 Python 代码:

class Shape(object):

    def draw(self):
        raise NotImplementedError


class Triangle(Shape):

    def draw(self):
        print("draw triangle")

class Square(Shape):

    def draw(self):
        print("draw square")

t = Triangle()

我想获取实例t 的类名(字符串)。

我怎样才能得到它?

【问题讨论】:

    标签: python class


    【解决方案1】:

    你可以使用instance.__class__.__name__获取类名:

    t = Triangle()
    
    print(t.__class__.__name__)
    

    【讨论】:

      猜你喜欢
      • 2020-06-19
      • 2022-06-15
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 2017-05-16
      • 2014-06-20
      • 1970-01-01
      相关资源
      最近更新 更多