u08_slides-classes-flashcards
What is Object-Oriented Programming (OOP)?
A programming paradigm based on objects that combines variables, functions, and data structures to increase code reusability and modularity
What are the two main components of objects in OOP?
- State (data/attributes/fields), 2. Behavior (methods/functions)
What is the difference between a class and an object?
A class is like a blueprint that defines attributes and methods, while an object is a specific instance of a class that exists uniquely
What is the naming convention for Python classes?
Class names should use CapWords convention (e.g.
What is the purpose of the __init__ method in a class?
It’s a special method for object initialization, called when creating a new instance of the class
Why must all object methods have ‘self’ as their first parameter?
‘self’ references the instance the method was called on, allowing access to instance attributes and methods
What’s the difference between class attributes and instance attributes?
Class attributes exist once and are shared by all instances, while instance attributes exist separately for each object instance
How do you create a new instance of a class?
Using MyClass(…) syntax, where … are arguments passed to __init__ (e.g., dog = Dog(‘Bello’))
How do you access class or object attributes?
Using dot notation: MyClass.attribute for class attributes, my_obj.attribute for instance attributes
What is inheritance in Python classes?
A mechanism where a class (subclass/child) can inherit attributes and methods from another class (superclass/parent)
How do you create a subclass in Python?
Using the syntax: class ChildClass(ParentClass), e.g., class GuardDog(Dog)
What is the purpose of the super() function?
It allows access to methods from the parent class, particularly useful in method overriding
What is Method Resolution Order (MRO)?
The order in which Python searches for methods through the class hierarchy, typically bottom-to-top in single inheritance
What is the purpose of isinstance(x
y)?
What is the purpose of issubclass(x
y)?