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
  • 521
  • 1
  • 6
  • 15

1 Answers1

2

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

t = Triangle()

print(t.__class__.__name__)
aircraft
  • 25,146
  • 28
  • 91
  • 166