StepsEasy
Python Classes & Objects - Example
Run
Reset
Destroy & Close
← Back to lesson
Code Editor
python
class Animal: def __init__(self, name, sound): self.name = name self.sound = sound def speak(self): return f"{self.name} says {self.sound}" class Dog(Animal): def __init__(self, name): super().__init__(name, 'Woof!') def fetch(self, item): return f"{self.name} fetches the {item}" dog = Dog('Rex') print(dog.speak()) # Rex says Woof! print(dog.fetch('ball')) # Rex fetches the ball
Result
Click "Run" to execute the code...
Loading runtime...
Ready
PYTHON | In-Browser Execution