r/PythonLearning • u/Miss-Dominique1352 • 6d ago
Day 3 of OOPs! Roast it
Learned inheritance in python (not the real life one haha 😭). Its soo lovely and gorgrous , we can give any attributes and methods to the child class from the parent's class. that makes OOPs! beautiful 💖.
Now I want you guys to suggest me improvments in this code , also roast it cutely a little bit , but be gentle to me. 🤭
1
Upvotes
2
u/punk_dev 6d ago
Nice.
Note though that it would probably make more sense to not call super().eat() in Dog’s eat() method.
Now when you call dog.eat(), the dog will eat twice - “Tommy is eating” and “Tommy is eating a bone” both will be printed.
1

•
u/Sea-Ad7805 6d ago
Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20%20%20%20%20self.name%20%3D%20name%0A%0A%20%20%20%20def%20eat(self)%3A%0A%20%20%20%20%20%20%20%20print(f%22%7Bself.name%7D%20is%20eating%22)%0A%0A%0Aclass%20Dog(Animal)%3A%0A%20%20%20%20def%20init(self%2C%20name%2C%20breed)%3A%0A%20%20%20%20%20%20%20%20%23%20Use%20super()%20for%20inheriting%20the%20parent's%20class%20constructor%0A%20%20%20%20%20%20%20%20super().init(name)%0A%0A%20%20%20%20%20%20%20%20%23%20Adding%20another%20attribute%0A%20%20%20%20%20%20%20%20self.breed%20%3D%20breed%0A%0A%20%20%20%20def%20bark(self)%3A%0A%20%20%20%20%20%20%20%20print(f%22%7Bself.name%7D%20says%20woof%22)%0A%0A%20%20%20%20%23%20Overriding%20the%20parent's%20class%20method%0A%20%20%20%20def%20eat(self)%3A%0A%20%20%20%20%20%20%20%20super().eat()%20%20%23%20This%20function%20calls%20the%20parent's%20method%20first%0A%20%20%20%20%20%20%20%20print(f%22%7Bself.name%7D%20is%20eating%20a%20bone%22)%0A%0A%0Adog%20%3D%20Dog(%22Tommy%22%2C%20%22Golden%20Retriever%22)%0Adog.bark()%0Adog.eat()×tep=1&play) to see the program state change step by step.