Object-Oriented Programming Part – 1 | 9/100 Days of Python Algo trading

Object-Oriented Programming

Congratulations on reaching this stage!

If you’re here, it means you’re truly dedicated to mastering Python. Directly below, you’ll find a quiz designed to help you reinforce what you’ve just learned. This isn’t just about recalling facts; it’s about deeply understanding the concepts. Take a moment to answer the questions and see how well you can apply the knowledge from the video. You’re doing great—every question you tackle brings you one step closer to becoming a Python expert!

Day 9: OOP Part1

1. Consider these code snippets: Snippet A: class A: ..., Snippet B: def func_a(): .... Which statement is true in Python, and why?
2. Which OOP feature enables code like this: class Shape: ... , class Square(Shape): ..., class Circle(Shape): ... , my_shape = Square(). Explain the mechanism involved.
3. Suppose you have a Person class. Instead of storing 'age' as a regular attribute, you decide to calculate it on the fly based on the current date and birthdate. Discuss how this embodies OOP principles.
4. You have a function calculate_area(shape), and you want to include this behavior within your various shape classes (e.g. Square, Circle). What's the most OOP-centric way to do this, and why?
5. You're designing a system where you have a Vehicle class, Car and Truck inheriting from it, and a Manufacturer class. An association needs to exist between Manufacturer and Vehicle. Which is most accurate and why?
6. What's the potential pitfall of only defining __eq__ for a custom class without also considering __hash__?
7. Could you explain a scenario where defining __setattr__ could be useful over manipulating object attributes directly?
8. Describe a situation where using @classmethod would be more appropriate than @staticmethod within a class.
9. Why does Python need the explicit self reference within object methods, compared to implicit 'this' in languages like Java or C++?
10. Your custom Number class needs to support the unary minus operator (e.g., -my_number). Which magic method is responsible for this?
11. What's the difference between implementing __add__ vs. implementing __radd__ for your custom Number class?
12. You want your Number class to support += augmented assignment. Do you need to implement a special method for this, and if so, which one?
13. To make your Number class participate in mixed-type arithmetic (e.g., my_number * 2.5), what's a key strategy?
14. Metaclasses can be used for which advanced technique?
15. Which of the following is NOT a common downside of using inheritance heavily in practice?
16. Consider the task of modeling 'shapes'. Which scenario might make you hesitate in favor of an OOP approach using inheritance, and why?
17. What might be an OOP alternative to deep inheritance hierarchies?
18. What's a key use case for abstract classes in Python using the abc module?
19. When modeling something like file I/O, where there are common operations (open, close, read, write) but the underlying implementation varies significantly, how could OOP help?
20. Which design pattern is commonly seen in Python's standard library and makes heavy use of the __iter__ and __next__ magic methods?