Python Inheritance

Python · Functions & OOP

Theory

Inheritance lets a new class (the child or subclass) reuse and extend an existing class (the parent or base class). Writing class Manager(Employee) means a Manager automatically has every attribute and method of Employee, and can add its own (team_size) or override a parent method by defining one with the same name.

super() gives access to the parent's version of a method. In Manager.__init__, super().__init__(name) lets Employee set self.name, then the child adds self.team_size. When you call a method on an object, Python searches the object's class first, then its parents in order (the method resolution order, visible via Manager.__mro__), and uses the first match it finds.

Why we use it

Inheritance avoids duplicating shared code. Common logic lives once in the parent, and each child adds only what is different, so a fix in the parent automatically reaches every subclass.

When to use it

Use inheritance for a true 'is-a' relationship (a Manager is an Employee). If one class merely uses another (a Car has an Engine), prefer composition - storing an object as an attribute - instead of deep inheritance chains.

How it works

  1. Call m.describe() on a Manager object
  2. Look for describe in class Manager
  3. Defined in Manager? Yes: Run the Manager version (override). No: Look in parent class Employee.
  4. Defined in Employee? Yes: Run the inherited version. No: Search up to object, else AttributeError.
  5. Method runs with self = m

Syntax

class Child(Parent):
    ...

Example

class Manager(Employee):
    def __init__(self, name, team_size):
        super().__init__(name)
        self.team_size = team_size

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic