Question 1 : How to check the type of the class object?
class car:
def __init__(self, name, brand):
self.name = name
self.brand = brand
class bus:
def __init__(self, name, brand):
self.name = name
self.brand = brand
a3 = car("Q3", "Audi")
v9 = bus("V9400", "Volvo")
print(type(a3))
print(type(v9))
Output:
<class '__main__.car'>
<class '__main__.bus'>
Question 2 : How to check if an object is the instance of the class?
class car:
def __init__(self, name, brand):
self.name = name
self.brand = brand
class bus:
def __init__(self, name, brand):
self.name = name
self.brand = brand
a3 = car("Q3", "Audi")
v9 = bus("V9400", "Volvo")
print(isinstance(a3, car))
print(isinstance(v9, car))
Output:
Question 3 : Define a common class attribute for all the classes.
class car:
brand = "Audi"
def __init__(self, name, engine):
self.name = name
self.engine = engine
q5 = car("Q5", "Pertol")
d6 = car("D6", "Diesel")
print(q5.brand)
print(q5.name)
print(d6.brand)
print(d6.engine)
Output:
© Copyright 2024 www.logicalpython.com. All rights reserved.
We constantly review Tutorials, references, and examples to avoid errors, but we cannot warrant full correctness
of all content.