I am going through Dave's Introduction to Classes video (https://www.youtube.com/watch?v=u4Ryk0YuW6A). I am a javascript / typescript guy, so python is somewhat new to me. I am using CodiumAI in vscode (the free version), and I wanted to see if it could create a list of questions to help me learn about Python classes and reinforce my understanding. Here is what it came up with. (The point of this post is to encourage you all to start exploring new ways to learn whatever it is you are trying to learn!) Python Classes Quiz
1. Basic Class Definition
- What is a class in Python, and how do you define one?
- Write a simple class named Car with an attribute color.
2. Class Attributes and Methods
- What is the difference between a class attribute and an instance attribute?
- How do you define a method inside a class?
- Create a class Dog with an instance attribute name and a method bark that prints "Woof!".
3. The __init__ Method
- What is the purpose of the __init__ method in a class?
- How do you initialize an instance attribute in the __init__ method?
- Write a class Book with attributes title and author, and initialize them using the __init__ method.
4. Creating and Using Instances
- How do you create an instance of a class?
- Given a class Person with an attribute name, how would you create an instance named john with the name "John Doe"?
5. Inheritance
- What is inheritance in the context of Python classes?
- How do you create a subclass that inherits from a superclass?
- Create a class Animal with a method speak, and a subclass Cat that overrides the speak method to print "Meow!".
6. Encapsulation
- What is encapsulation, and how is it implemented in Python?
- How can you make an attribute private in a Python class?
- Modify the Car class to include a private attribute _mileage.
7. Polymorphism
- What is polymorphism in Python, and how does it relate to classes?
- Provide an example of polymorphism using a method describe in two different classes.
8. Class vs. Instance Methods
- What is the difference between a class method and an instance method?
- How do you define a class method, and when would you use it?
- Add a class method from_string to the Book class that creates an instance from a string formatted as "title,author".
9. Special Methods
- What are special methods in Python, and why are they useful?
- Explain the purpose of the __str__ method and provide an example using the Book class.
10. Static Methods
- What is a static method, and how does it differ from class and instance methods?
- When would you use a static method in a class?
- Add a static method is_valid_isbn to the Book class that checks if a given ISBN is valid.
Feel free to use these questions to test your understanding or as a guide to explore Python classes further. If you need explanations or answers to any of these questions, let me know!