Python OOP: Classes & Inheritance

60 min2 pages

What is Python OOP: Classes & Inheritance?

Learning concept: Basic object-oriented programming in Python: classes, instances, methods, attributes, and simple inheritance

~60 min2 pages
PythonProgrammingBeginner

OBJECT-ORIENTED PROGRAMMING IN PYTHON: AN INTRODUCTION\n\nObject-oriented programming (OOP) is a way of modeling real-world entities as software objects. In Python, you create classes to define a blueprint, and then you instantiate objects from that blueprint. A class groups data attributes (state) and methods (behavior) that operate on that data. Through this structure, you build programs that mirror real-world relationships and promote code reuse. In this page we cover the basics: defining a class, creating instances, adding attributes, implementing methods, and exploring simple inheritance.\n\n## Core Idea\n- Classes serve as templates for objects.\n- Instances are concrete realizations of a class.\n- Methods are functions defined inside a class that operate on its data.\n- Attributes store the state of an object.\n- Inheritance lets one class extend another, reusing and extending behavior.\n\n> Key principle: encapsulation groups data and behavior, while an object’s interface exposes what others can rely on.\n\nThis foundation makes Python programs more modular, easier to test, and adaptable as projects grow.

Which statement best describes a class in Python?

A function that performs a specific task
A template or blueprint for creating objects
A built-in data type like int or str
An external library module

Sign up to unlock quizzes

Example: Simple Class Template

python
class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        return f'{self.name} makes a sound.'

What method name is commonly used to initialize a new object in Python classes?

__new__
__init__
__start__
__begin__

Sign up to unlock quizzes

The _____ keyword is used within a class to set up initial attributes when an object is created

Type your answer...

Sign up to unlock quizzes

Example: Creating an Instance

python
class Car:
    def __init__(self, model):
        self.model = model

my_car = Car('Sedan')
print(my_car.model)  # Sedan

Python objects expose attributes and methods through dot notation. An attribute stores data about an object, and a method is a function tied to an object that can access and modify its attributes. For example, a Person object might have attributes like name and age, and methods like greet() or celebrate_birthday(). When you call a method, Python implicitly passes the object as the first parameter (conventionally named self). This enables methods to read and modify the object’s own data while remaining encapsulated inside the class. Understanding how to design attributes versus methods helps produce clear, maintainable code. In simple terms: attributes describe, methods act. Inheritance adds the ability for a class to derive from another, inheriting its attributes and methods, which we will explore in a later page.

In Python, what is the first parameter of an instance method typically named?

cls
self
this
object

Sign up to unlock quizzes

Example: Accessing Attributes in a Method

python
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

r = Rectangle(4, 5)
print(r.area())  # 20

Which of the following best describes an attribute?

A function bound to a class that performs actions
A value stored on an object representing its state
A special method used to construct objects
A module that defines related classes

Sign up to unlock quizzes

Example: Adding an Attribute Later

python
class BankAccount:
    def __init__(self, balance):
        self.balance = balance

account = BankAccount(100)
print(account.balance)  # 100
account.balance += 50
print(account.balance)  # 150

Simple inheritance lets a new class share behavior from an existing class. The new class (subclass) inherits attributes and methods from the base class (superclass) and can add or override functionality. For example, a Vehicle class might define wheels and move(), while a Car subclass could reuse move() and add features like honk(). Inheritance promotes code reuse and a natural hierarchy of concepts. When designing classes, prefer composing behavior over deep inheritance hierarchies, keep subclass responsibilities focused, and document how the base features are extended. This foundation sets the stage for more advanced topics like method resolution order and super(), which enable cooperative multiple inheritance.

You've previewed 1 of 2 pages

Sign up free to unlock the rest of this lesson and start tracking your progress.

1 more page waiting:

  • Page 2

Related Topics