Python classes, inheritance, and object-oriented concepts explained with a short example each.
40 cards · basic cards · AI-written, checked twice. Edit anything.
- What is a class in Python?
Show answer
A blueprint or template for creating objects with shared attributes and methods
- How does an object relate to a class?
Show answer
An object is a concrete instance of a class containing actual data
- What Python keyword is used to define a class?
Show answer
class
- What is the __init__ method in a class?
Show answer
The constructor that is automatically called when a new instance is created
- What does the self parameter represent in a method?
Show answer
A reference to the current instance of the class
- What is an instance attribute?
Show answer
An attribute that belongs to a single object and can differ across instances
- What is a class attribute?
Show answer
An attribute defined in the class body that is shared by all instances
- What is inheritance in object-oriented programming?
Show answer
A mechanism where a child class acquires the attributes and methods of a parent class
- How do you create a child class that inherits from a parent?
Show answer
class ChildClass(ParentClass):
- What is method overriding?
Show answer
Redefining a method from the parent class in the child class with different behavior
- What does super() do?
Show answer
Calls a method in the parent class from within the child class
- What is encapsulation in OOP?
Show answer
Bundling data and methods together and hiding internal implementation details
- What does a single underscore prefix on an attribute mean?
Show answer
By convention, it signals the attribute is intended to be private or internal
- What does a double underscore prefix on an attribute do?
Show answer
It triggers name mangling to make the attribute harder to access from outside the class
- What does the @property decorator allow?
Show answer
Defining a method that can be accessed and used like an attribute with dot notation