class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return 'Some sound'
def info(self):
return f'{self.name} says {self.speak()}'
class Dog(Animal):
def speak(self):
return 'Woof!'
dog = Dog('Rex')
print(dog.name) # 'Rex' (inherited from Animal)
print(dog.speak()) # 'Woof!' (overridden)
print(dog.info()) # 'Rex says Woof!' (inherited method calls overridden speak)class Person:
def __init__(self, name, age):
self.name = name
self.age = age
class Student(Person):
def __init__(self, name, age, school):
super().__init__(name, age) # Call parent's __init__
self.school = school
s = Student('Ali', 20, 'MIT')
print(s.name) # 'Ali'
print(s.school) # 'MIT'class List:
def append(self, item):
print(f'Appending {item}')
class LogList(List):
def append(self, item):
print(f'[LOG] Before append')
super().append(item) # Call parent's append
print(f'[LOG] After append')
ll = LogList()
ll.append(5)
# [LOG] Before append
# Appending 5
# [LOG] After appendclass A:
def greet(self):
return 'A'
class B(A):
def greet(self):
return 'B'
class C(A):
def greet(self):
return 'C'
class D(B, C):
pass
d = D()
print(d.greet()) # 'B'
print(D.__mro__)
# (<class 'D'>, <class 'B'>, <class 'C'>, <class 'A'>, <class 'object'>)print(isinstance(d, D)) # True
print(isinstance(d, B)) # True
print(isinstance(d, A)) # True
print(issubclass(D, A)) # True
print(issubclass(A, D)) # FalseCreate a base class `Vehicle` with `__init__(self, brand, speed)` and a method `describe()` returning a string. Create `Car(Vehicle)` that adds `doors` attribute via super().__init__() and overrides `describe()` to include doors. Create `ElectricCar(Car)` that adds `battery` attribute. Demonstrate all three levels work. Also print `ElectricCar.__mro__`.