2

I have below Python code:

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()

I want to get the instance t's class's name(string).

How can I get it?

fanhualuojin154873
  • 441
  • 1
  • 5
  • 14

1 Answers1

2

You can use instance.__class__.__name__ to get the class name:

t = Triangle()

print(t.__class__.__name__)
aircraft
  • 20,821
  • 22
  • 79
  • 154