Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to print the string name of a class that inherits attributes and methods from a parent class

I couldn’t find a solution to this anywhere, but I’ve used a method in my parent class (Wallet) to print the name of the class when I call the method function in the script.

The problem is that the name is still showing as that of the parent class, even when i have created a ‘Pen’ class to inherit the attributes and methods of the ‘Wallet’ class.

class Wallet():
    
    def __init__(self,colour,size,material,cost):
        print("A Wallet Object Class has been made")
        self.colour = colour
        self.size = size
        self.material = material
        self.cost = cost

    def colour_call(self):
        print(f"The colour of the {__class__.__name__} is {self.colour}")
    def size_call(self):
        print(f"The size of the {__class__.__name__} is {self.size}")
    def material_call(self):
        print(f"The material is made of {self.material}")
    def cost_call(self):
        print(f"The cost of the {__class__.__name__} is {self.cost}")

Here i used the __class__.__name__ to refer to the name of the class itself.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Pen(Wallet):

    def __init__(self, colour, size, material, cost):
        super().__init__(colour, size, material, cost)
        print(f"A {__class__.__name__} Object Class has inherited from the Wallet Class")

I then created this pen class to inherit the same attributes and methods as the Wallet class.

mywallet = Wallet(colour = "red", size = "Large", material = "leather", cost = 2.50)
mypen = Pen(colour = "blue", size = "small", material = "aluminium", cost = 1.50)

for item in [mywallet,mypen]:
    item.colour_call()
    item.size_call()
    item.material_call()

here when i try to use polymorphism to return the method calls, it prints the following:

The colour of the Wallet is red
The size of the Wallet is Large
The material is made of leather
The colour of the Wallet is blue
The size of the Wallet is small
The material is made of aluminium

Even for the Pen class, it is using the class name of the Wallet. I was expecting it to be ‘Pen’ (The size of the Pen is small…) Any ideas?

It works if I just call the instance of the attributes and manually print the result like below, but I wanted to do it that way just for curiosity.

for item in [mywallet,mypen]:
    print(f"The colour of the {item.__class__.__name__} is "+item.colour+".",
         f"The material of the {item.__class__.__name__} is "+item.material+".")

>Solution :

Don’t use the special variable __class__, the whole point of that variable is to provide the class that the method is defined in. It exists to support the zero-argument form of super.

Use:

type(self).__name__

or the equivalent

self.__class__.__name__ 

It isn’t clear why you used the special variable __class__ to begin with, it’s a rather arcane piece of trivia to know it even exists. Anyway, the above approaches are the way to get the class of an arbitrary object, you already seem to know it yourself since you correctly used item.__class__!

As an aside, you inheritance hierarchy doesn’t make sense conceptually, a pen is not a wallet, and so it shouldn’t inherit from wallet. You probably want your Wallet class to be a base Item class or something like that, that both Wallet and Pen inherit from.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading