Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    PyPython

    Getting started

    • Your first Python program
    • Variables and types
    • Numbers and operators
    • Type conversion and input

    Strings

    • Strings and f-strings
    • String indexing and slicing
    • Cleaning and splitting strings

    Control flow

    • Conditions: if, elif and else
    • Loops: for and while
    • Loop control and nesting

    Functions

    • Functions with def
    • Parameters and scope

    Collections

    • Lists
    • Dictionaries
    • Tuples and sets
    • Comprehensions, enumerate and zip

    Errors, files and libraries

    • Handling exceptions
    • Modules and the standard library
    • Reading and writing files
    • Exchanging data with JSON

    Objects and lazy iteration

    • Classes and objects
    • Inheritance and composition
    • Iterators and generators

    Testing and project

    • Testing and project setup
    • Project: a grade report
    Python

    Objects and lazy iteration · Lesson 22 of 25

    Inheritance and composition

    Reuse behavior with an appropriate relationship.

    8 minutes2 games

    Inheritance models “is a”: class Student(Person). A subclass may add or override methods; super() follows the method resolution order. Composition stores another object as a field and often better represents “has a”.

    Python
    class Person:
        def __init__(self, name):
            self.name = name
    
        def greet(self):
            return f"Hi {self.name}"
    
    class Student(Person):
        def __init__(self, name, level):
            super().__init__(name)
            self.level = level
    
    student = Student("Mona", 2)
    print(student.greet())
    print(student.level)
    • Person
      • Student
      • Teacher

    Important

    Do not inherit solely to save lines. If the object is not a kind of the parent, consider passing a helper object instead.

    Interview question

    How do overriding and calling super differ?

    Quick question

    A car has an engine. Which relationship is clearer?

    Exercise

    Make Dog inherit Animal and override sound to return woof. Print Dog().sound().

    Expected output: woof

    Python
    class Animal:
        def sound(self):
            return "?"
    
    

    Learn more from

    • Python documentationdocs.python.org
    • Real Python: further readingrealpython.com

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousClasses and objectsNextIterators and generators
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.