0

I made a method and called onto it with super() but I do not know how to write the name of the class in a print statement!

from abc import ABC, abstractmethod

class Clothing(ABC):
    @abstractmethod
    def desc(self):
        pass


class Shirt(Clothing):
    def desc(self):
        x = input('Enter a color: ')
        y = input('Enter the size: ')

        print(f"Your {self} is: {x}\nAnd its size is: {y}")


class Jean(Shirt):
    def desc(self):
        super().desc()


# shirt = Shirt()
# shirt.desc()

jean = Jean()
jean.desc()

I tried printing self, which, while it does kind of return the class name, it has this answer:

Your **<__main__.Jean object at 0x0000023E2C2D7D30>** is: red
And its size is: 32

Btw I started learning like a week ago so please do enlighten me

Dorian Turba
  • 3,260
  • 3
  • 23
  • 67
Jecise
  • 3
  • 2

1 Answers1

0

instance.__class__.__name__

You need to access the __class__ attribute to get the class, then __name__ to get the class name.

class Foobar:
    ...

print(Foobar().__class__.__name__)
# Foobar

With your code, here is an example of usage:

class Jean(Shirt):
    def desc(self):
        super().desc()

    def class_name(self):
        return self.__class__.__name__

jean = Jean()
print(jean.class_name())
# Jean
Dorian Turba
  • 3,260
  • 3
  • 23
  • 67