Is there Python Class instance have an attribute to get the class name?

Viewed 59

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?

1 Answers

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

t = Triangle()

print(t.__class__.__name__)
Related