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.

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+".")

0

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.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật