
Get a class name in Python - How to use class and name
Python instance (object) has the __class__
attribute and it has the __name__
attribute, which is the class name of an instance.
class Product:
def __init__(self, price):
self.price = price
p = Product(price=25)
c = p.__class__
n = p.__class__.__name__
print(c) # <class '__main__.Product'>
print(n) # Product
Comments
Powered by Markdown